我对如何解决一个特殊问题感到困惑。基本上,我希望做到以下几点:
使用pandas,我希望遍历行,如果字段中的值为None/NaN,则将字段名称附加到新字段中,如下所示
+----+--------+----------+--------+--------+--------+---------------------------------+
| ID | Animal | Building | Letter | Fruit | Number | NullFields |
+----+--------+----------+--------+--------+--------+---------------------------------+
| 1 | Dog | House | C | null | 4 | Fruit |
| 2 | null | House | null | Apple | null | Animal, Letter, Number |
| 3 | Cat | null | B | Orange | null | Building, Number |
| 4 | null | null | null | null | 6 | Animal, Building, Letter, Fruit |
| 5 | Snake | null | A | null | 7 | Building, Fruit |
+----+--------+----------+--------+--------+--------+---------------------------------+
为了便于阅读,我在上面输入了“null”。我知道None/NaN不一样,但我处理的数据似乎两者都有。如果我必须运行fillna
,那很好
我认为np.where
在这里不起作用,除非我遗漏了什么。我不知道我是否需要用iterrows
来代替或者什么
任何提示/指导都将不胜感激
首先需要使
NaN
字段为true,以度量它们是否为null,然后我们可以使用isnull
后跟.dot
这样做可以:
输出:
相关问题 更多 >
编程相关推荐