从 pandas 中的字符串中删除字符 [英] Removing characters from a string in pandas

查看:79
本文介绍了从 pandas 中的字符串中删除字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有一个类似的问题: 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/','')

这篇关于从 pandas 中的字符串中删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆