mk2080
mk2080

Reputation: 922

Filling every Nan in an entire Dataframe

I'm trying to fill every blank in a dataframe.

I tried:

df_without_nan = df.fillna("D")

#then I got an error about category so I added this

df_without_nan = df.cat.add_categories("D").fillna("D")

#cat.add only works on a series....

for column in df: 
    df[column] = df[column]cat.add_categories("D").fillna("D")

This is probably intuitive... help!

Upvotes: 0

Views: 72

Answers (1)

jezrael
jezrael

Reputation: 863146

You are close, loop by categorical columns by DataFrame.select_dtypes:

df = pd.DataFrame({
        'A':list('abc'),
         'B':[np.nan,'A','B'],
         'C':[7,8,9],
         'D':['X','Y', np.nan],

})

df['B'] = df['B'].astype('category')
print (df)
   A    B  C    D
0  a  NaN  7    X
1  b    A  8    Y
2  c    B  9  NaN

 #first add category to categorical columns
for column in df.select_dtypes('category'): 
    df[column] = df[column].cat.add_categories("D")

#then replace all NaNs
df = df.fillna("D")   
print (df)
   A  B  C  D
0  a  D  7  X
1  b  A  8  Y
2  c  B  9  D

Upvotes: 3

Related Questions