简体   繁体   English

如何在pandas.groupby函数中为每个组创建一个数据框?

[英]How to create a data frame for each group in the pandas.groupby function?

I have a data frame which has been grouped by 'hour'. 我有一个已按“小时”分组的数据框。 I want to use a for loop which loops through each group and creates a data frame for each group. 我想使用一个for循环,该循环遍历每个组并为每个组创建一个数据帧。 The code I'm using at the moment only creates a data frame for the last group that it iterates through. 目前,我正在使用的代码仅为其循环访问的最后一个组创建一个数据框。 Any suggestions on how I can get it to work properly? 关于如何使它正常工作的任何建议?

for name, group in data.groupby('hour'):
    d = {'group_' + str(name) : group}

You're overwriting your object each time you want this: 每当您想要这样做时,您都将覆盖对象:

d = {}
for name, group in data.groupby('hour'):
    d['group_' + str(name)] = group

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM