这是一个示例熊猫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'
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 200
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句