1

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.

Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
RJL
  • 341
  • 1
  • 7
  • 19
  • this should work, can you provide a data sample? – Benoit de Menthière Oct 16 '19 at 22:34
  • This is an almost duplicate of https://stackoverflow.com/questions/38134012/pandas-dataframe-fillna-only-some-columns-in-place you can find loads of useful variants there. – Davide Fiocco Oct 16 '19 at 22:36
  • This is not a duplicate because the posting you mentioned is referring to the columns by name. My question was referring to the columns by position backward. As I mentioned in my original question, my code works when I referred to the columns by specific names. – RJL Oct 17 '19 at 16:10

1 Answers1

3

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)
Davide Fiocco
  • 5,350
  • 5
  • 35
  • 72
  • 1
    just out of curiosity, why doesn't 'inplace=True' work? – RJL Oct 16 '19 at 22:43
  • 1
    I'd refer you to https://stackoverflow.com/questions/55744015/iloc-fillna-inplace-true-vs-inplace-false and links therein! As far as I understand, that inplace is updating a copy and not the original `df` – Davide Fiocco Oct 16 '19 at 22:48