【发布时间】:2019-01-17 00:33:52
【问题描述】:
我有一个包含 3 列的数据框:Role、to_group1、to_group2、remove,我想分配True,其中to_group1 和to_group2 中的值是nan,但是看来我的代码不起作用,我做错了什么?
df.remove = np.where(((df.to_group1 == np.nan)) & ((df.to_group2 ==
np.nan)), True, np.nan)
使用此代码,我只能得到 remove 的列满 nan。
这是我的表格的一个例子:
+------+-----------+-----------+--------+
| role | to_group1 | to_group2 | remove |
+------+-----------+-----------+--------+
| foo | nan | 1 | nan |
+------+-----------+-----------+--------+
| foo1 | nan | nan | 1 |
+------+-----------+-----------+--------+
| bar | 1 | nan | nan |
+------+-----------+-----------+--------+
此外,我已经用一些值初始化了我的列remove,我不想为整个列重新分配新值,我只想“在两个条件都满足的地方设置一个 true”并且不修改任何内容否则。
【问题讨论】:
-
你想要
df.remove = np.where((pd.isnull(aws.to_group1)) & (pd.isnull(aws.to_group2)), True, np.nan)你不能和NaN直接使用==比较
标签: python pandas numpy dataframe