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 columnproduct_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
Credit To: stackoverflow.com
Related Query
- Series object has no attribute 'strip'
- Object pandas has no attribute name Series
- pandas plot time series ['numpy.ndarray' object has no attribute 'find']
- Pandas' series contains AttributeError: 'Series' object has no attribute 'contains'
- Series object has no split attribute - reading in data from text file
- Getting attribute error: Series object has no attribute 'explode'
- AttributeError: 'Series' object has no attribute 'reshape'
- Pythonic/efficient way to strip whitespace from every Pandas Data frame cell that has a stringlike object in it
- Data-frame Object has no Attribute
- 'module' object has no attribute 'DataFrame'
- AttributeError: 'DataFrame' object has no attribute
- 'DataFrame' object has no attribute 'as_matrix
- AttributeError: 'ElementTree' object has no attribute 'getiterator' when trying to import excel file
- Pandas to_sql to sqlite returns 'Engine' object has no attribute 'cursor'
- AttributeError: 'Series' object has no attribute 'as_matrix' Why is it error?
- AtributeError: 'module' object has no attribute 'plt' - Seaborn
- Error: float object has no attribute notnull
- Pandas - 'Series' object has no attribute 'colNames' when using apply()
- pyspark error: AttributeError: 'SparkSession' object has no attribute 'parallelize'
- AttributeError: 'Series' object has no attribute 'iterrows'
- Bokeh: AttributeError: 'DataFrame' object has no attribute 'tolist'
- DataFrame object has no attribute 'sort_values'
- How to fix AttributeError: 'Series' object has no attribute 'to_numpy'
- How to solve the Attribute error 'float' object has no attribute 'split' in python?
- Unpickling dictionary that holds pandas dataframes throws AttributeError: 'Dataframe' object has no attribute '_data'
- str.contains pandas returns 'str' object has no attribute 'contains'
- AttributeError: 'str' object has no attribute 'view' in Seaborn , Scatterplot
- pandas - 'dataframe' object has no attribute 'str'
- Error in reading stock data : 'DatetimeProperties' object has no attribute 'weekday_name' and 'NoneType' object has no attribute 'to_csv'
- "DataFrame" object has no attribute 'reshape'
More Query from same tag
- divide value of next row and create column in dataframe
- Converting a List of Pandas Series to a single Pandas DataFrame
- pandas, groupby.agg to return with a given column name
- remove URL id from URL in pandas column to leave the base url without id
- How to replace DataFrame.append with pd.concat to append a Series as row?
- is it possible to define the index of character in sentence with str.contain?
- Best method for sending large pandas dataframe to SQL database?
- Sort outer multi-index
- following line take lot of time to update since it has nearly 2.5l records are present
- for each row get frequency of the most frequent value
- How to assign a value to a new column with a string condition in pandas dataframe
- Merging two dataframes that share a date column
- seaborn: Selected KDE bandwidth is 0. Cannot estimate density
- Fill zero values for combinations of unique multi-index values after groupby
- How to vectorize pandas operation