pandas 系列对象没有属性 'strip'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35657918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:46:34  来源:igfitidea点击:

Series object has no attribute 'strip'

pythonpandasdataframe

提问by JoeBlack

Here is a sample pandas DataFrame:

这是一个示例Pandas数据帧:

id  product_type    qty
1   product_type 1  100
2   product_type 2  300
3   product_type 1  200

I want to delete product_typein the column product_typein order obtain the following new DataFrame:

我想product_type在列product_type中删除以获得以下新数据帧:

id  product_type    qty
1   1               100
2   2               300
3   1               200

This is how I tried to do it:

这就是我尝试这样做的方式:

orders['product_type'].strip('product_type ')

However there is an error:

但是有一个错误:

'Series' object has no attribute 'strip'

回答by EdChum

you need .strin front of it as it's a string accessor method:

你需要.str在它前面,因为它是一个字符串访问器方法

orders['product_type'].str.strip('product_type ')



In [6]:
df['product_type'] = df['product_type'].str.strip('product_type ')
df

Out[6]:
   id product_type  qty
0   1            1  100
1   2            2  300
2   3            1  200

Or pass a regex to extract the numbers to str.extract:

或者传递一个正则表达式来提取数字str.extract

In [8]:
df['product_type'] = df['product_type'].str.extract(r'(\d+)')
df

Out[8]:
   id product_type  qty
0   1            1  100
1   2            2  300
2   3            1  200