How to group DataFrame by a period of time?

后端 未结 3 1581
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: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')])
    

提交回复
热议问题