的GroupBy结果列表
的词典我有一个Excel工作表,看起来像这样:的GroupBy结果列表
Column1 Column2 Column3
0 23 1
1 5 2
1 2 3
1 19 5
2 56 1
2 22 2
3 2 4
3 14 5
4 59 1
5 44 1
5 1 2
5 87 3
我期待通过第1列,以提取数据,组,然后将其添加到字典,以便它出现这样的:
{0: [1],
1: [2,3,5],
2: [1,2],
3: [4,5],
4: [1],
5: [1,2,3]}
这是到目前为止我的代码
excel = pandas.read_excel(r"e:\test_data.xlsx", sheetname='mySheet', parse_cols'A,C')
myTable = excel.groupby("Column1").groups
print myTable
然而,我的输出看起来是这样的:
{0: [0L], 1: [1L, 2L, 3L], 2: [4L, 5L], 3: [6L, 7L], 4: [8L], 5: [9L, 10L, 11L]}
谢谢!
然后取Column3
至apply(list)
并致电to_dict
?
In [81]: df.groupby('Column1')['Column3'].apply(list).to_dict()
Out[81]: {0: [1], 1: [2, 3, 5], 2: [1, 2], 3: [4, 5], 4: [1], 5: [1, 2, 3]}
根据the docs,所述GroupBy.groups
:
是一个字典的键是计算唯一的组和相应的 值是属于各组的轴标签。
如果你想值本身,你可以groupby
“列1”,然后调用apply
并通过list
方法应用到每个组。
您可以根据需要,然后将其转换为一个字典:
In [5]:
dict(df.groupby('Column1')['Column3'].apply(list))
Out[5]:
{0: [1], 1: [2, 3, 5], 2: [1, 2], 3: [4, 5], 4: [1], 5: [1, 2, 3]}
(注:看看this SO question为什么数字后面是L
),你可以groupby
上Column1
非常感谢您的回复! – SuperDougDougy
@SuperDougDougy你现在可以upvote ;-) – EdChum
这当然!谢谢! – SuperDougDougy
当@ EdChum的完全相同并且提前3分钟发布时,意思是接受这个答案。 – LondonRob