这是一个熊猫DataFrame示例:
id product_type qty
1 product_type 1 100
2 product_type 2 300
3 product_type 1 200我想删除product_type列中的product_type,以便获得以下新的DataFrame:
id product_type qty
1 1 100
2 2 300
3 1 200我试着这样做:
orders['product_type'].strip('product_type ')但是有一个错误:
'Series' object has no attribute 'strip'发布于 2016-02-27 01:22:09
因为它是一个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或者传递正则表达式以将数字提取到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 200https://stackoverflow.com/questions/35657918
复制相似问题