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'
The Python "AttributeError: 'list' object has no attribute 'strip'" occurs when we call the strip() method on a list instead of a string. To solve the error, call strip() on a string, e.g. by accessing the list at a specific index or by iterating over the list.
The remove() method is one of the ways you can remove elements from a list in Python. The remove() method removes an item from a list by its value and not by its index number.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With