从熊猫中的字符串中删除字符
问题描述:
我对此有一个类似的问题: Pandas DataFrame:从一列中的字符串中删除不需要的部分.
I have a similar question to this one: Pandas DataFrame: remove unwanted parts from strings in a column.
所以我用过:
temp_dataframe['PPI'] = temp_dataframe['PPI'].map(lambda x: x.lstrip('PPI/'))
大多数项目都以"PPI/"开头,但并非全部.似乎当没有'PPI/'后缀的项目遇到此错误时:
Most, of the items start with a 'PPI/' but not all. It seems that when an item without the 'PPI/' suffix encountered this error:
AttributeError:"float"对象没有属性"lstrip"
AttributeError: 'float' object has no attribute 'lstrip'
我在这里想念东西吗?
答
使用替换:
temp_dataframe['PPI'].replace('PPI/','',regex=True,inplace=True)
或 string.replace :
temp_dataframe['PPI'].str.replace('PPI/','')