【发布时间】:2019-02-10 00:57:03
【问题描述】:
我有一个数据框,其中有一列包含值列表。数据框中的每一行都有一个相同长度的列表。我想使用 Dataframe.groupby 对数据框中的数据进行分组,并按以下方式将列表汇总在一起:
在:
import pandas as pd
#Sample data
a = pd.DataFrame([['a', 'test', list([0,1,2,3,4])],['b', 'test', list([5,6,7,8,9])]], columns=['id', 'grp', 'values'])
print(a)
#Some function to group the dataframe
#b = a.groupby('grp').someAggregationFunction()
#Example of desired output
b = pd.DataFrame([['test', list([5,7,9,11,13])]], columns=['grp', 'values'])
print(b)
输出:
id grp values
0 a test [0, 1, 2, 3, 4]
1 b test [5, 6, 7, 8, 9]
grp values
0 test [5, 7, 9, 11, 13]
【问题讨论】:
标签: python pandas numpy pandas-groupby