"pandas: how to convert a cell with multiple values to multiple rows?" Code Answer

2

carrying on from the same idea, you could set a multiindex for df2 and then stack. for example:

>>> df2 = df.asn.str.split(',').apply(pd.series)
>>> df2.index = df.set_index(['name', 'count']).index
>>> df2.stack().reset_index(['name', 'count'])
   name  count     0
0  org1      1  asn1
1  org1      1  asn2
0  org2      2  asn3
0  org3      5  asn4
1  org3      5  asn5

you can then rename the column and set an index of your choosing.

By Aaron Montgomery on August 8 2022

Answers related to “pandas: how to convert a cell with multiple values to multiple rows?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.