How to split a pandas dataframe or series by day (possibly using an iterator)

后端 未结 2 2036
耶瑟儿~
耶瑟儿~ 2021-01-08 00:17

I have a long time series, eg.

import pandas as pd
index=pd.date_range(start=\'2012-11-05\', end=\'2012-11-10\', freq=\'1S\').tz_localize(\'Europe/Berlin\')         


        
相关标签:
2条回答
  • 2021-01-08 00:45

    If you want to group by date (AKA: year+month+day), then use df.index.date:

    result = [group[1] for group in df.groupby(df.index.date)]
    

    As df.index.day will use the day of the month (i.e.: from 1 to 31) for grouping, which could result in undesirable behavior if the input dataframe dates extend to multiple months.

    0 讨论(0)
  • 2021-01-08 00:46

    Perhaps groupby?

    DFList = []
    for group in df.groupby(df.index.day):
        DFList.append(group[1])
    

    Should give you a list of data frames where each data frame is one day of data.

    Or in one line:

    DFList = [group[1] for group in df.groupby(df.index.day)]
    

    Gotta love python!

    0 讨论(0)
提交回复
热议问题