How to group DataFrame by a period of time?

后端 未结 3 1566
Happy的楠姐
Happy的楠姐 2020-11-30 02:40

I have some data from log files and would like to group entries by a minute:

 def gen(date, count=10):
     while count > 0:
         yield date, \"event{         


        
相关标签:
3条回答
  • 2020-11-30 03:22

    pd.TimeGrouper is now depreciated. Here is v1.05 update using pd.Grouper

    df['Date'] = df.index
    
    df.groupby(['Source',pd.Grouper(key = 'Date', freq='30min')])
    
    0 讨论(0)
  • 2020-11-30 03:30

    Since the original answer is rather old and pandas introduced periods a different solution is nowadays:

    df.groupby(df.index.to_period('T'))
    

    Additionally, you can resample

    df.resample('T')
    
    0 讨论(0)
  • 2020-11-30 03:44

    You can group on any array/Series of the same length as your DataFrame --- even a computed factor that's not actually a column of the DataFrame. So to group by minute you can do:

    df.groupby(df.index.map(lambda t: t.minute))
    

    If you want to group by minute and something else, just mix the above with the column you want to use:

    df.groupby([df.index.map(lambda t: t.minute), 'Source'])
    

    Personally I find it useful to just add columns to the DataFrame to store some of these computed things (e.g., a "Minute" column) if I want to group by them often, since it makes the grouping code less verbose.

    Or you could try something like this:

    df.groupby([df['Source'],pd.TimeGrouper(freq='Min')])
    
    0 讨论(0)
提交回复
热议问题