DumbCoder
DumbCoder

Reputation: 445

Split rows based on multiple columns pandas

Is there a way to split rows into multiple rows based on some column values?

My input dataframe is

   A              B           C
0  1  1234.0,5643.0     One,Two
1  2           5432  Three,Four
2  3  1278.0,5678.0        Five

I want to split the column B and C with ','. The output dataset to should be:

   A       B      C
0  1  1234.0    One
0  1  1234.0    Two
0  1  5643.0    One
0  1  5643.0    Two
1  2    5432  Three
1  2    5432   Four
2  3  1278.0   Five
2  3  5678.0   Five

Upvotes: 1

Views: 939

Answers (2)

Alex
Alex

Reputation: 7045

Using str.split to turn the strings into lists then explode each one:

df["B"] = df["B"].str.split(",")
df["C"] = df["C"].str.split(",")
for col in ["B", "C"]:
    df = df.explode(col)

#    A       B      C
# 0  1  1234.0    One
# 0  1  1234.0    Two
# 0  1  5643.0    One
# 0  1  5643.0    Two
# 1  2    5432  Three
# 1  2    5432   Four
# 2  3  1278.0   Five
# 2  3  5678.0   Five

In pandas v1.3.0 you can do a multi-column explode, but this requires lists be the same length in all columns.

Upvotes: 4

HARSH MITTAL
HARSH MITTAL

Reputation: 760

You can use:

pd.concat([df[[0]], df[1].str.split(', ', expand=True)], axis=1)

More reference: Pandas split column into multiple columns by comma

Upvotes: 0

Related Questions