'Split a string column and put the splits in different columns
import pandas as pd
df = pd.read_csv("product_2022-02-10.csv")
df["primary_category_name_en"]
Result:
primary_category_name_en
Gadgets & Electronics > Desktop Computer & Computer Accessories > Connecting Cable & Unit Convertor > Lan Line
Electrical Appliances > Other Small Home Appliances & Accessories > Drill & Extension Cord & Battery > Battery & Battery Charger > Other Battery
Housewares > Hardware & Tools > Metal Tools & Hardware Tools > Switch
Electrical Appliances > Smart Home > Smart Extension Cord
How can I split by > and put it back to different columns?
I know how to split it. df["primary_category_name_en”].str.split(">")
There are some only 3 categories in a row / 4 categories in a row / 5 categories in a row. I don't know how to solve the problem in this.
For example:
ROW 1:
df["Primary_category_1"] = "Gadgets & Electronics"
df["Primary_category_2"] = "Desktop Computer & Computer Accessories"
df["Primary_category_3"] = "Connecting Cable & Unit Convertor"
df["Primary_category_4"] = "Lan Line"
ROW 2:
df["Primary_category_1"] = "Electrical Appliances"
df["Primary_category_2"] = "Other Small Home Appliances & Accessories"
df["Primary_category_3"] = "Drill & Extension Cord & Battery"
df["Primary_category_4"] = "Battery & Battery Charger"
df["Primary_category_5"] = "Other Battery"
ROW 3:
...
ROW4:
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
