Peter
Peter

Reputation: 383

Pandas: apply to all columns in dataframe

For all cells in my dataframe, I just want to replace whitespace or empty (not NAN) with 0. Tried the below, but doesn't work.

Thank you

df1 = df1.all(apply(lambda x: 0 if x == ' ' or x=='' else x))

Upvotes: 0

Views: 66

Answers (1)

BENY
BENY

Reputation: 323226

Try with replace

df1 = df1.replace({' ':0,'':0})

or Change your function from apply to mask

df1 = df1.mask(df1.isin([' ', '']),0)

Upvotes: 4

Related Questions