替换熊猫数据框x;如果布尔数组中的各个索引为True,则y值为NaN

时间:2018-09-26 18:09:12

标签: python pandas numpy dataframe

给出数据框:

coords           x           y    likelihood
0       197.486369    4.545954  3.890000e-07
1       319.946460  191.035224  2.360000e-07
2       319.880388  191.012984  2.740000e-07
3       320.286005  190.843329  2.170000e-07
4       320.210989  190.863304  2.490000e-07
5       320.212529  190.867178  2.310000e-07
...            ...         ...           ...
10461   349.995217  303.397438  9.999956e-01
10462   349.109666  305.710711  9.665585e-01
10463   352.142571  310.081763  9.994751e-01
10464   351.916488  317.078128  1.792100e-02
10465   353.809847  320.086683  9.277161e-01
10466   349.233529  321.859424  9.466683e-03

我怎么x;如果布尔数组中的各个索引为True,则y值为NaN。

1 个答案:

答案 0 :(得分:2)

假设您的布尔数组如下所示:

arr = np.array([True, False, True, False, True, False])

使用loc

df.loc[arr, ['x', 'y']] = np.nan

   coords           x           y    likelihood
0       0         NaN         NaN  3.890000e-07
1       1  319.946460  191.035224  2.360000e-07
2       2         NaN         NaN  2.740000e-07
3       3  320.286005  190.843329  2.170000e-07
4       4         NaN         NaN  2.490000e-07
5       5  320.212529  190.867178  2.310000e-07