asn
asn

Reputation: 2587

Handle missing data for a dataframe column of datatype object

I have a pandas dataframe and one of the column is of datatype object . There is a blank element present in this column, so I tried to check if there are other empty element in this column by using df['colname'].isnull().sum() but it is giving me 0. How can I replace the above value(empty) with some arbitrary value(numeric) so that I can convert this column into a column of float datatype for further computation.

Upvotes: 1

Views: 76

Answers (1)

piRSquared
piRSquared

Reputation: 294218

pandas.to_numeric

df['colname'] = pd.to_numeric(df['colname'], errors='coerce')

This will produce np.nan for any thing it can't convert to a number. After this, you can fill in with any value you'd like with fillna

df['colname'] = df['colname'].fillna(0)

All in one go

df['colname'] = pd.to_numeric(df['colname'], errors='coerce').fillna(0)

Upvotes: 2

Related Questions