如何根据入口条件过滤已经包含 NaN 的数据框?

我从一个数据帧开始,我想根据输入值的条件将它分成三个数据帧。对于 df1,我只想让 Person_X 值 <0.33。对于 df2,我希望只有 0.34<Person_X 值<0.65。对于 df3,我只想让 Person_X 值 >0.66。我希望 NaN 替换不符合要求的整数值。


所以,从 df 开始:


In [1]: df=pd.DataFrame({'location':[4991, 8870, 2703, 9674], 

   ...:                  'Person_1': ['NaN', 0.2,0.5,0.7], 

   ...:                  'Person_2':[0.8, 0.45, 'NaN', 0.1]}) 

   ...: df                                                                      

Out[1]: 

   location Person_1 Person_2

0      4991      NaN      0.8

1      8870      0.2     0.45

2      2703      0.5      NaN

3      9674      0.7      0.1

我想要三个看起来像的数据框:


 df1

   location Person_1 Person_2

0      4991      NaN      NaN

1      8870      0.2     NaN

2      2703      NaN     NaN

3      9674      NaN      0.1


df2

 Out[4]: 

   location Person_1 Person_2

0      4991      NaN      NaN

1      8870      NaN     0.45

2      2703      0.5      NaN

3      9674      NaN      NaN


df3

 Out[4]: 

   location Person_1 Person_2

0      4991      NaN      0.8

1      8870      NaN      NaN

2      2703      NaN      NaN

3      9674      0.7      NaN

我已经针对每个要求尝试过类似的方法,但是由于 df 已经包含 NaN,所以我遇到了问题。


patient_cols=[col for col in df if col.startswith('Person')]

df[patient_cols]=df[patient_cols].applymap(lambda x: np.nan if x>0.33 else x)

df[patient_cols]


慕勒3428872
浏览 90回答 1
1回答

烙印99

让我们使用pd.DataFrame.where:df=pd.DataFrame({'location':[4991, 8870, 2703, 9674],&nbsp;&nbsp; &nbsp;'Person_1': [np.nan, 0.2,0.5,0.7],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Person_2':[0.8, 0.45, np.nan, 0.1]})&nbsp;&nbsp;#Just incase your NaN are strings as indicated by your original posts.df=df.replace('NaN', np.nan)&nbsp;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)输出:&nbsp; &nbsp;location&nbsp; Person_1&nbsp; Person_20&nbsp; &nbsp; &nbsp; 4991&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN1&nbsp; &nbsp; &nbsp; 8870&nbsp; &nbsp; &nbsp; &nbsp;0.2&nbsp; &nbsp; &nbsp; &nbsp;NaN2&nbsp; &nbsp; &nbsp; 2703&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN3&nbsp; &nbsp; &nbsp; 9674&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;0.1&nbsp; &nbsp;location&nbsp; Person_1&nbsp; Person_20&nbsp; &nbsp; &nbsp; 4991&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN1&nbsp; &nbsp; &nbsp; 8870&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; 0.452&nbsp; &nbsp; &nbsp; 2703&nbsp; &nbsp; &nbsp; &nbsp;0.5&nbsp; &nbsp; &nbsp; &nbsp;NaN3&nbsp; &nbsp; &nbsp; 9674&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp;location&nbsp; Person_1&nbsp; Person_20&nbsp; &nbsp; &nbsp; 4991&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;0.81&nbsp; &nbsp; &nbsp; 8870&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN2&nbsp; &nbsp; &nbsp; 2703&nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp;NaN3&nbsp; &nbsp; &nbsp; 9674&nbsp; &nbsp; &nbsp; &nbsp;0.7&nbsp; &nbsp; &nbsp; &nbsp;NaN
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python