Search code examples
pythonpandasfillna

How to apply fillna to last N columns of a pandas dataframe?


I have a data frame with many columns. I would like to fill the nan's with 0's for the last x number of columns. I used the following code but it doesn't seem to work.

df.iloc[:, -10:-1].fillna(value=0, inplace=True)

What am I doing wrong? when I specifically refer to a column by name:

df['column_a'].fillna(value=0, inplace=True)

it works, but I'd rather not do it one column at a time.


Solution

  • A recipe that should work here is

    df.iloc[:, -x:] = df.iloc[:, -x:].fillna(value=0)
    

    A reproducible example here is

    import pandas as pd
    df = pd.DataFrame({'col1':range(10),
                       'col2':range(1, 11), 
                       'col3':range(2, 12),
                       'col4':range(3, 13),
                       'col5':range(4, 14)})
    
    # pepper with NaNs
    df.iloc[8, 2] = None
    df.iloc[8, 3] = None
    df.iloc[8, 4] = None
    
    # apply fillna change to last 2 cols
    x = 2
    df.iloc[:, -x:] = df.iloc[:, -x:].fillna(value=0)
    
    print(df)