Skip to main content
 首页 » 编程设计

python之从 Pandas 中的字符串中删除字符

2025年05月04日21飞鱼

我有一个类似的问题:Pandas DataFrame: remove unwanted parts from strings in a column .

所以我用了:

temp_dataframe['PPI'] = temp_dataframe['PPI'].map(lambda x: x.lstrip('PPI/')) 

大多数项目以“PPI/”开头,但不是全部。好像是当没有'PPI/'后缀的项目遇到这个错误:

AttributeError: 'float' object has no attribute 'lstrip'

我是不是漏掉了什么?

请您参考如下方法:

使用replace :

temp_dataframe['PPI'].replace('PPI/','',regex=True,inplace=True) 

或string.replace :

temp_dataframe['PPI'].str.replace('PPI/','')