Pandas groupby month and year

后端 未结 4 1414
闹比i
闹比i 2020-11-27 11:18

I have the following dataframe:

Date        abc    xyz
01-Jun-13   100    200
03-Jun-13   -20    50
15-Aug-13   40     -5
20-Jan-14   25     15
21-Feb-14   6         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 11:50

    You can also do it by creating a string column with the year and month as follows:

    df['date'] = df.index
    df['year-month'] = df['date'].apply(lambda x: str(x.year) + ' ' + str(x.month))
    grouped = df.groupby('year-month')
    

    However this doesn't preserve the order when you loop over the groups, e.g.

    for name, group in grouped:
        print(name)
    

    Will give:

    2007 11
    2007 12
    2008 1
    2008 10
    2008 11
    2008 12
    2008 2
    2008 3
    2008 4
    2008 5
    2008 6
    2008 7
    2008 8
    2008 9
    2009 1
    2009 10
    

    So then, if you want to preserve the order, you must do as suggested by @Q-man above:

    grouped = df.groupby([df.index.year, df.index.month])
    

    This will preserve the order in the above loop:

    (2007, 11)
    (2007, 12)
    (2008, 1)
    (2008, 2)
    (2008, 3)
    (2008, 4)
    (2008, 5)
    (2008, 6)
    (2008, 7)
    (2008, 8)
    (2008, 9)
    (2008, 10)
    

提交回复
热议问题