Pandas Dataframe object types fillna exception over different datatypes

You can grab the float64 and object columns using:

In [11]: float_cols = df.blocks['float64'].columns

In [12]: object_cols = df.blocks['object'].columns

and int columns won't have NaNs else they would be upcast to float.

Now you can apply the respective fillnas, one cheeky way:

In [13]: d1 = dict((col, '') for col in object_cols)

In [14]: d2 = dict((col, 0) for col in float_cols)

In [15]: df.fillna(value=dict(d1, **d2))

You can iterate through them and use an if statement!

for col in df:
    #get dtype for column
    dt = df[col].dtype 
    #check if it is a number
    if dt == int or dt == float:
        df[col].fillna(0)
    else:
        df[col].fillna("")

When you iterate through a pandas DataFrame, you will get the names of each of the columns, so to access those columns, you use df[col]. This way you don't need to do it manually and the script can just go through each column and check its dtype!


A compact version example:

#replace Nan with '' for columns of type 'object'
df=df.select_dtypes(include='object').fillna('') 

However, after the above operation, the dataframe will only contain the 'object' type columns. For keeping all columns, use the solution proposed by @Ryan Saxe.

Tags:

Python

Pandas