Home > Mobile >  Explode pandas dataframe column values to separate rows
Explode pandas dataframe column values to separate rows

Time:02-02

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

Any help is highly appreciated.

CodePudding user response:

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

CodePudding user response:

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
  • Related