Iterating over groups in a dataframe [duplicate]

試著忘記壹切 提交于 2019-12-02 11:54:18

When you apply groupby on a dataframe, you don't get rows, you get groups of dataframe. For example, consider:

df
    ID        Date  Days  Volume/Day
0  111  2016-01-01    20          50
1  111  2016-02-01    25          40
2  111  2016-03-01    31          35
3  111  2016-04-01    30          30
4  111  2016-05-01    31          25
5  112  2016-01-01    31          55
6  112  2016-01-02    26          45
7  112  2016-01-03    31          40
8  112  2016-01-04    30          35
9  112  2016-01-05    31          30

for i, g in df.groupby('ID'):
     print(g, '\n')


    ID        Date  Days  Volume/Day
0  111  2016-01-01    20          50
1  111  2016-02-01    25          40
2  111  2016-03-01    31          35
3  111  2016-04-01    30          30
4  111  2016-05-01    31          25 

    ID        Date  Days  Volume/Day
5  112  2016-01-01    31          55
6  112  2016-01-02    26          45
7  112  2016-01-03    31          40
8  112  2016-01-04    30          35
9  112  2016-01-05    31          30 

For your case, you should probably look into dfGroupby.apply, if you want to apply some function on your groups, dfGroupby.transform to produce like indexed dataframe (see docs for explanation) or dfGroupby.agg, if you want to produce aggregated results.

You'd do something like:

r = df.groupby('Date').apply(your_function) 

You'd define your function as:

def your_function(df):
    ... # operation on df
    return result

If you have problems with the implementation, please open a new question, post your data and your code, and any associated errors/tracebacks. Happy coding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!