raptorzee
raptorzee

Reputation: 167

How to split dataframe made out of lists into different columns?

I have the following dataframe. All the content in between the inverted commas are in one column. I want to split them into separate columns.:

df=

0,"#1            Microwave Oven Sharp 20 Litres, White, R-20AS-W        5.0 out of 5 stars3SAR 199.00 "
1,"#2            Nikai Microwave - 20 LTR -NMO515N8N        5.0 out of 5 stars3SAR 177.00" 
2,"#3            Geepas 20 Liter Microwave Oven - GMO1894        SAR 186.00" 

I want to split it into columns like

df=
0,"#1",            "Microwave Oven Sharp 20 Litres, White, R-20AS-W",        "5.0 out of 5 stars3", "SAR 199.00 "
    1,"#2",            "Nikai Microwave - 20 LTR -NMO515N8N",        "5.0 out of 5 stars3", "SAR 177.00" 
    2,"#3",            "Geepas 20 Liter Microwave Oven - GMO1894",        "SAR 186.00" 

Upvotes: 0

Views: 48

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Use .str.split with are regex for two more more spaces and parameter expand=True:

df[column_name].str.split('\s\s+', expand=True)

Upvotes: 1

Aswin Babu
Aswin Babu

Reputation: 348

in place of columnname mention your orginal columnname.

df["new_col"]=df["columnname"].astype(str).str.split("            ")[0]
df["new_col2"]=df["columnname"].astype(str).str.split("            ")[1]
df["new_col3"]=df["columnname"].astype(str).str.split("            ")[2]

Upvotes: 0

Related Questions