Pandas: how to convert a cell with multiple values to multiple rows?

点点圈 提交于 2019-12-20 14:13:13

问题


I have a DataFrame like this:

Name asn  count
Org1 asn1,asn2 1
org2 asn3      2
org3 asn4,asn5 5

I would like to convert my DataFrame to look like this:

Name asn  count
Org1 asn1 1
Org1 asn2 1 
org2 asn3 2
org3 asn4 5
Org3 asn5 5

I know used the following code to do it with two columns, but I am not sure how can I do it for three.

df2 = df.asn.str.split(',').apply(pd.Series)          
df2.index = df.Name                                   
df2 = df2.stack().reset_index('Name') 

Can anybody help?


回答1:


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.




回答2:


As an alternative:

import pandas as pd
from StringIO import StringIO

ctn = '''Name asn count
Org1 asn1,asn2 1
org2 asn3      2
org3 asn4,asn5 5'''

df = pd.read_csv(StringIO(ctn), sep='\s*', engine='python')
s = df['asn'].str.split(',').apply(pd.Series, 1).stack()
s.index = s.index.droplevel(-1)
s.name = 'asn'
del df['asn']
df = df.join(s)

print df

Result:

   Name  count   asn
0  Org1      1  asn1
0  Org1      1  asn2
1  org2      2  asn3
2  org3      5  asn4
2  org3      5  asn5


来源:https://stackoverflow.com/questions/29605352/pandas-how-to-convert-a-cell-with-multiple-values-to-multiple-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!