Search code examples
pythonpandasfillna

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


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 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.)

Data before:

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

Data after:

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

Solution

  • 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