Here is a sample pandas DataFrame:

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:

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'

score:27

Accepted answer

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:

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

Related Query