我有一个类似的问题: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:
或string.replace:
使用矢量化
str.lstrip
:看起来您可能缺少值,因此应将这些值屏蔽掉或替换为:
或者
也许更好的方法是使用
str.startswith
进行筛选,并使用split
访问要删除的前缀后面的字符串:正如@JonClements指出的,
lstrip
正在删除空白,而不是删除前缀,这正是您所追求的。更新
另一种方法是传递一个正则表达式模式,该模式查找可选前缀并提取前缀后的所有字符:
相关问题 更多 >
编程相关推荐