Pandas:如何将具有多个值的单元格转换为多行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29605352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: how to convert a cell with multiple values to multiple rows?
提问by UserYmY
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:
我想将我的 DataFrame 转换为如下所示:
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?
有人可以帮忙吗?
回答by Alex Riley
Carrying on from the same idea, you could set a MultiIndex for df2and then stack. For example:
按照同样的想法,您可以设置一个 MultiIndex df2,然后堆叠。例如:
>>> 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 Vor
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

