有没有办法在df中删除单个条目?
df1:
ID Count1 Value1 Count2 Value2
111 5 10 5 25
112 4 15 6 25
113 6 7 4 10
114 3 8 2 1
115 7 1 8 10
如果Count1<我想删除任何Value1如果Count2< 5,则删除任何Value2。结果是这样的:
ID Count1 Value1 Count2 Value2
111 5 10 5 25
112 NA NA 6 25
113 6 7 4 NA
114 NA NA NA NA
115 7 1 8 10
谢谢!
答案 0 :(得分:2)
使用loc
df.loc[df['Count1'] < 5, ['Count1','Value1']] = np.nan
df.loc[df['Count2'] < 5, ['Count2','Value2']] = np.nan
ID Count1 Value1 Count2 Value2
0 111 5.0 10.0 5.0 25.0
1 112 NaN NaN 6.0 25.0
2 113 6.0 7.0 NaN NaN
3 114 NaN NaN NaN NaN
4 115 7.0 1.0 8.0 10.0
答案 1 :(得分:1)
使用mask
:
df[['Count1','Value1']] = df[['Count1','Value1']].mask(df.Count1 < 5)
df[['Count2','Value2']] = df[['Count2','Value2']].mask(df.Count2 < 5)
print (df)
ID Count1 Value1 Count2 Value2
0 111 5.0 10.0 5.0 25.0
1 112 NaN NaN 6.0 25.0
2 113 6.0 7.0 NaN NaN
3 114 NaN NaN NaN NaN
4 115 7.0 1.0 8.0 10.0
答案 2 :(得分:1)
试试这个:
df1 = df1 [df1 ['Count1']&lt; 5&amp; df1 ['Count2']&lt; 5]