How to pass another entire column as argument to pandas fillna()

后端 未结 6 2182
太阳男子
太阳男子 2020-11-22 07:01

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

(I read that looping through each row would be

6条回答
  •  时光取名叫无心
    2020-11-22 07:28

    Just use the value parameter instead of method:

    In [20]: df
    Out[20]:
      Cat1      Cat2  Day
    0  cat     mouse    1
    1  dog  elephant    2
    2  cat     giraf    3
    3  NaN       ant    4
    
    In [21]: df.Cat1 = df.Cat1.fillna(value=df.Cat2)
    
    In [22]: df
    Out[22]:
      Cat1      Cat2  Day
    0  cat     mouse    1
    1  dog  elephant    2
    2  cat     giraf    3
    3  ant       ant    4
    

提交回复
热议问题