Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace WhiteSpace with a 0 in Pandas (Python 3)

Tags:

python

pandas

simple question here -- how do I replace all of the whitespaces in a column with a zero?

For example:

  Name       Age
  John       12
  Mary 
  Tim        15

into

  Name       Age
  John       12
  Mary       0
  Tim        15

I've been trying using something like this but I am unsure how Pandas actually reads whitespace:

 merged['Age'].replace(" ", 0).bfill()

Any ideas?

like image 360
user3682157 Avatar asked Jun 12 '26 09:06

user3682157


1 Answers

merged['Age'] = merged['Age'].apply(lambda x: 0 if x == ' ' else x)
like image 187
paulo.filip3 Avatar answered Jun 14 '26 23:06

paulo.filip3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!