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
Series object has no attribute 'strip'
提问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_type
in the column product_type
in 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 .str
in front of it as it's a string accessor method:
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