3

I know codes forfilling seperately by taking each column as below

data['Native Country'].fillna(data['Native Country'].mode(), inplace=True)

But i am working on a dataset with 50 rows and there are 20 categorical values which need to be imputed. Is there a single line code for imputing the entire data set??

Antony Joy
  • 301
  • 3
  • 15

1 Answers1

2

Use DataFrame.fillna with DataFrame.mode and select first row because if same maximum occurancies is returned all values:

data = pd.DataFrame({
        'A':list('abcdef'),
         'col1':[4,5,4,5,5,4],
         'col2':[np.nan,8,3,3,2,3],
         'col3':[3,3,5,5,np.nan,np.nan],
         'E':[5,3,6,9,2,4],
         'F':list('aaabbb')
})

cols = ['col1','col2','col3']

print (data[cols].mode())
   col1  col2  col3
0     4   3.0   3.0
1     5   NaN   5.0

data[cols] = data[cols].fillna(data[cols].mode().iloc[0])
    
print (data)
   A  col1  col2  col3  E  F
0  a     4   3.0   3.0  5  a
1  b     5   8.0   3.0  3  a
2  c     4   3.0   5.0  6  a
3  d     5   3.0   5.0  9  b
4  e     5   2.0   3.0  2  b
5  f     4   3.0   3.0  4  b
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Could you explain why we are using iloc[0] ? – Antony Joy Mar 10 '21 at 14:02
  • 1
    @AntonyJoy - If check `print (data[cols].mode())` there are multiple modes for columns `col1` and `col3` (because here 3 times 4, 5 in `col1` and 2 times 4,5 in `col3` what is maximum number of counts), and need replace only by one value, so used first value. – jezrael Mar 10 '21 at 14:05