如何将另一整列作为参数传递给 pandas fillna() [英] How to pass another entire column as argument to pandas fillna()

查看:31
本文介绍了如何将另一整列作为参数传递给 pandas fillna()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 fillna 方法用另一列中的值填充一列中的缺失值.

I would like to fill missing values in one column with values from another column, using fillna method.

(我读到循环遍历每一行将是非常糟糕的做法,最好一次性完成所有事情,但我无法找到如何使用 fillna 来做到这一点.)

(I read that looping through each row would be very bad practice and that it would be better to do everything in one go but I could not find out how to do it with fillna.)

之前的数据:

Day  Cat1  Cat2
1    cat   mouse
2    dog   elephant
3    cat   giraf
4    NaN   ant

之后的数据:

Day  Cat1  Cat2
1    cat   mouse
2    dog   elephant
3    cat   giraf
4    ant   ant

推荐答案

您可以将此列提供给 fillna(请参阅 docs),它将使用匹配索引上的这些值来填充:

You can provide this column to fillna (see docs), it will use those values on matching indexes to fill:

In [17]: df['Cat1'].fillna(df['Cat2'])
Out[17]:
0    cat
1    dog
2    cat
3    ant
Name: Cat1, dtype: object

这篇关于如何将另一整列作为参数传递给 pandas fillna()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆