1

I Can not remove white spaces from DataFrame. Tried next path without success: .replace(' ', '')

The csv file - https://drive.google.com/file/d/1AjlFYxGPUqVFkxW6QXA6hNRjkLR8eTFP/view?usp=sharing

Please, help me solve that. The problematic column is 'gdp_per_capita'

2
  • 2
    df['gdp_per_capita'].str.replace('\s+', '', regex=True).astype(int) Commented Feb 5, 2021 at 8:07
  • @Ferris Perfect. Thanks Commented Feb 5, 2021 at 8:09

1 Answer 1

1

For replacing the white space from a string in series or dataframe object use the str.replace() method.

For Ex:

>>> import pandas as pd
>>> ser = pd.Series(['aaa ','aac vf','cc  '])
>>> ser
0      aaa 
1    aac vf
2      cc  
dtype: object

>>> ser[0]
'aaa '

>>> len(ser[0])
4

>>> ser.str.replace(" ", "")
0      aaa
1    aacvf
2       cc
dtype: object

As you can see, the replace method removed the white spaces form string.

Sign up to request clarification or add additional context in comments.

Comments