Search code examples
pythonpandasfillna

fill Nas with other column in pandas


I have some NAs and would like to fill them with values in another column in the same row.

df = pd.DataFrame({"column A": ["Atlanta", "Atlanta", "New York", "",""], 
                   "column B": ["AT", "AT", "NY", "1", ""]})
columnA ColumnB
Atlanta AT
Atlanta AT
Newyork NY
1

Expected output:

columnA ColumnB
Atlanta AT
Atlanta AT
Newyork NY
1 1

Solution

  • You can Simply Use the fillna with the Column Name like this:

    df['column A'].fillna(df['column B'])