Explode pandas dataframe column values to separate rows

Question:

I have following pandas dataframe:

id          term            code
2445 | 2716 abcd | efgh     2345
1287        hgtz            6567

I would like to explode ‘id’ and ‘term’ column. How can I explode multiple columns to keep the values across the column (id, term, code) together.

The expected output is:

id          term            code
2445        abcd            2345
2716        efgh            2345
1287        hgtz            6567

I have tried so far is:
df.assign(id=df['id'].str.split(' | ')).explode('id')

Asked By: rshar

||

Answers:

You’re in the right way, you just need some help from concat with a listcomp :

out = (
        pd.concat([df[col].str.split("s*|s*")
                          .explode() for col in ["id", "term"]], axis=1)
             .join(df["code"])
      )

Output :

print(out)

     id  term  code
0  2445  abcd  2345
0  2716  efgh  2345
1  1287  hgtz  6567
Answered By: Timeless

here is a way using .str.split() and explode() which can accept multiple columns

(df[['id','term']].stack()
.str.split(' | ',regex=False)
.unstack()
.explode(['id','term'])
.join(df[['code']]))

Output:

     id  term  code
0  2445  abcd  2345
0  2716  efgh  2345
1  1287  hgtz  6567
Answered By: rhug123
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.