1 回答
TA贡献1829条经验 获得超13个赞
让我们使用pd.DataFrame.where
:
df=pd.DataFrame({'location':[4991, 8870, 2703, 9674],
'Person_1': [np.nan, 0.2,0.5,0.7],
'Person_2':[0.8, 0.45, np.nan, 0.1]})
#Just incase your NaN are strings as indicated by your original posts.
df=df.replace('NaN', np.nan)
df= df.set_index('location')
df1 = df.where(df<.33).reset_index()
df2 = df.where((df>.34) & (df<.65)).reset_index()
df3 = df.where(df>.66).reset_index()
print(df1)
print('\n')
print(df2)
print('\n')
print(df3)
输出:
location Person_1 Person_2
0 4991 NaN NaN
1 8870 0.2 NaN
2 2703 NaN NaN
3 9674 NaN 0.1
location Person_1 Person_2
0 4991 NaN NaN
1 8870 NaN 0.45
2 2703 0.5 NaN
3 9674 NaN NaN
location Person_1 Person_2
0 4991 NaN 0.8
1 8870 NaN NaN
2 2703 NaN NaN
3 9674 0.7 NaN
添加回答
举报