Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas Split strings into two Columns using str.split()

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'"

like image 940
adventureicecoffee Avatar asked Oct 27 '25 07:10

adventureicecoffee


1 Answers

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)
like image 100
Marco Avatar answered Oct 29 '25 21:10

Marco