Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Series object has no attribute 'strip'

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'
like image 911
JoeBlack Avatar asked Feb 26 '16 17:02

JoeBlack


People also ask

Has no attribute Strip Python?

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.

How do you strip an object from a list in Python?

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.


1 Answers

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
like image 75
EdChum Avatar answered Oct 04 '22 13:10

EdChum