Shashank
Shashank

Reputation: 103

Replace whitespace in pandas dataframe with 0 in all columns

I am having a dataframe, df with 15 columns. These columns contains scores of products. I want to replace all blank values(white spaces) here with 0. How to proceed here.

Upvotes: 0

Views: 451

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24314

Try via dataframe.replace():

df=df.replace({'':0,' ':0,float('NaN'):0})

OR

df=df.replace(r'\s*',0,regex=True).fillna(0)

Sample Dataframe:

df=pd.DataFrame({'col1':[56,42,'',' ',float('NaN')],'col2':[float('NaN'),45,'',' ',89]})

Upvotes: 1

Related Questions