似乎无法将一个数据帧上的重复项删除,但可以将其删除

时间:2019-03-11 01:17:29

标签: python pandas numpy

对于使用同一代码的一个Pandas数据帧,我的drop_duplicates()似乎有效,而对于另一个则不起作用。我似乎无法弄清楚。

无法正常工作的情况:

df_select
df_select= #Cont on the next line     
df_select.drop_duplicates(subset='RoundDown',keep='first',inplace=False)

df_select

enter image description here

投稿重复的情况:

df_select1 = pd.DataFrame( {'RoundDown': 
               [0.1,0.2,0.3,0.3],'RoundUp':[0.01,np.nan,0.4,np.nan]})
df_select1

df_select1 = 
df_select1.drop_duplicates(subset='RoundDown',keep='first',inplace=False)
df_select1

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以fillna并使用duplicated

df_select1=df_select1[~df_select1.RoundUp.fillna('NaN').duplicated()]
df_select1
Out[212]: 
   RoundDown  RoundUp
0    0.10000  0.01000
1    0.20000      nan
2    0.30000  0.40000