首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Series对象没有属性'strip‘

Series对象没有属性'strip‘
EN

Stack Overflow用户
提问于 2016-02-27 01:21:08
回答 1查看 31.8K关注 0票数 11

这是一个熊猫DataFrame示例:

代码语言:javascript
复制
id  product_type    qty
1   product_type 1  100
2   product_type 2  300
3   product_type 1  200

我想删除product_type列中的product_type,以便获得以下新的DataFrame:

代码语言:javascript
复制
id  product_type    qty
1   1               100
2   2               300
3   1               200

我试着这样做:

代码语言:javascript
复制
orders['product_type'].strip('product_type ')

但是有一个错误:

代码语言:javascript
复制
'Series' object has no attribute 'strip'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-27 01:22:09

因为它是一个string accessor method,所以你需要在它前面使用.str

代码语言:javascript
复制
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

或者传递正则表达式以将数字提取到str.extract

代码语言:javascript
复制
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
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35657918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档