How do you split the text in a column to create a new column in a dataframe using "(" and ")"? Current data frame:
| Item | Description | |
|---|---|---|
| 0 | coat | Boys (Target) | 
| 1 | boots | Womens (DSW) | 
| 2 | socks | Girls (Kohls) | 
| 3 | shirt | Mens (Walmart) | 
| 4 | boots | Womens (DSW) | 
| 5 | coat | Boys (Target) | 
What I want to create:
| Item | Description | Retailer | |
|---|---|---|---|
| 0 | coat | Boys | Target | 
| 1 | boots | Womens | DSW | 
| 2 | socks | Girls | Kohls | 
| 3 | shirt | Mens | Walmart | 
| 4 | boots | Womens | DSW | 
| 5 | coat | Boys | Target | 
I've tried the following:
df[['Description'], ['Retailer']] = df['Description'].str.split("(")
I get an error: "TypeError: unhashable type: 'list'"
Hi I have run this tiny test and seems to work; note the space and the \ in the split string.
import pandas as pd
df = pd.Series(['Boys (Target)','Womens (DSW)','Girls (Kohls)'])
print(df)
d1 = df.str.split(' \(')
print(d1)
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