简体   繁体   中英

Python remove … and spaces from a column

I have a dataset with a column having values:

.. >= 7 years
1 <= ... < 4 years
4 <= ... < 7 years
4 <= ... < 7 years
1 <= ... < 4 years
1 <= ... < 4 years

I am trying to remove the spaces & also the "..."

I have used the following codes:

data.columns = data.columns.map(lambda x: x.lstrip('...'))

This removes the .. from the first line but not for the other lines. In order to remove the spaces I tried the following:

data.columns = data.columns.map(lambda x: x.strip()) 

But this doesn't seem to work. Can someone help.

Thanks in advance.

You can use replace to replace dots and spaces with empty string. In your case use:

data.columns = data.columns.map(lambda x: x.replace('.', '').replace(' ', '')) 

Pandas DataFrame.replace()接受正则表达式,因此您可以使用以下命令删除字符:

df = df.replace(r"\s*\.*", "", regex = True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM