如何在保留所有原始数据的同时聚合 pandas Dataframe?
How do I aggregate a pandas Dataframe while retaining all original data?
我的目标是聚合一个 pandas DataFrame,按标识字段对行进行分组。值得注意的是,除了平均值、标准差等汇总统计信息外,我还想保留 DataFrame 中的所有信息,而不仅仅是收集该组的汇总统计信息。我已经通过大量迭代执行了此转换,但我正在寻找cleaner/more pythonic 方法。值得注意的是,每个组的重复次数可能多于或少于 2 个,但所有组的重复次数始终相同。
示例:我想翻译以下格式
df = pd.DataFrame([
["group1", 4, 10],
["group1", 8, 20],
["group2", 6, 30],
["group2", 12, 40],
["group3", 1, 50],
["group3", 3, 60]],
columns=['group','timeA', 'timeB'])
print(df)
group timeA timeB
0 group1 4 10
1 group1 8 20
2 group2 6 30
3 group2 12 40
4 group3 1 50
5 group3 3 60
转换为以下格式的 df:
target = pd.DataFrame([
["group1", 4, 8, 6, 10, 20, 15],
["group2", 6, 12, 9, 30, 45, 35],
["group3", 1, 3, 2, 50, 60, 55]
], columns = ["group", "timeA.1", "timeA.2", "timeA.mean", "timeB.1", "timeB.2", "timeB.mean"])
print(target)
group timeA.1 timeA.2 timeA.mean timeB.1 timeB.2 timeB.mean
0 group1 4 8 6 10 20 15
1 group2 6 12 9 30 45 35
2 group3 1 3 2 50 60 55
最后,列名是什么并不重要,这些只是为了让示例更清楚。谢谢!
编辑:正如用户在评论中所建议的那样,我尝试了链接 Q/A 中的解决方案但没有成功:
df.insert(0, 'count', df.groupby('group').cumcount())
df.pivot(*df)
TypeError: pivot() takes from 1 to 4 positional arguments but 5 were given
试试 pivot_table
:
out = (df.assign(col=df.groupby('group').cumcount()+1)
.pivot_table(index='group', columns='col',
margins='mean', margins_name='mean')
.drop('mean')
)
out.columns = [f'{x}.{y}' for x,y in out.columns]
输出:
timeA.1 timeA.2 timeA.mean timeB.1 timeB.2 timeB.mean
group
group1 4.0 8.0 6.0 10 20 15
group2 6.0 12.0 9.0 30 40 35
group3 1.0 3.0 2.0 50 60 55
我的目标是聚合一个 pandas DataFrame,按标识字段对行进行分组。值得注意的是,除了平均值、标准差等汇总统计信息外,我还想保留 DataFrame 中的所有信息,而不仅仅是收集该组的汇总统计信息。我已经通过大量迭代执行了此转换,但我正在寻找cleaner/more pythonic 方法。值得注意的是,每个组的重复次数可能多于或少于 2 个,但所有组的重复次数始终相同。
示例:我想翻译以下格式
df = pd.DataFrame([
["group1", 4, 10],
["group1", 8, 20],
["group2", 6, 30],
["group2", 12, 40],
["group3", 1, 50],
["group3", 3, 60]],
columns=['group','timeA', 'timeB'])
print(df)
group timeA timeB
0 group1 4 10
1 group1 8 20
2 group2 6 30
3 group2 12 40
4 group3 1 50
5 group3 3 60
转换为以下格式的 df:
target = pd.DataFrame([
["group1", 4, 8, 6, 10, 20, 15],
["group2", 6, 12, 9, 30, 45, 35],
["group3", 1, 3, 2, 50, 60, 55]
], columns = ["group", "timeA.1", "timeA.2", "timeA.mean", "timeB.1", "timeB.2", "timeB.mean"])
print(target)
group timeA.1 timeA.2 timeA.mean timeB.1 timeB.2 timeB.mean
0 group1 4 8 6 10 20 15
1 group2 6 12 9 30 45 35
2 group3 1 3 2 50 60 55
最后,列名是什么并不重要,这些只是为了让示例更清楚。谢谢!
编辑:正如用户在评论中所建议的那样,我尝试了链接 Q/A 中的解决方案但没有成功:
df.insert(0, 'count', df.groupby('group').cumcount())
df.pivot(*df)
TypeError: pivot() takes from 1 to 4 positional arguments but 5 were given
试试 pivot_table
:
out = (df.assign(col=df.groupby('group').cumcount()+1)
.pivot_table(index='group', columns='col',
margins='mean', margins_name='mean')
.drop('mean')
)
out.columns = [f'{x}.{y}' for x,y in out.columns]
输出:
timeA.1 timeA.2 timeA.mean timeB.1 timeB.2 timeB.mean
group
group1 4.0 8.0 6.0 10 20 15
group2 6.0 12.0 9.0 30 40 35
group3 1.0 3.0 2.0 50 60 55