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

查看:36
本文介绍了如何将另一整列作为参数传递给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(请参见

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天全站免登陆