Pandas - Split dataframe into multiple dataframes based on dates?

前端 未结 2 1636
北荒
北荒 2020-12-10 08:26

I have a dataframe with multiple columns along with a date column. The date format is 12/31/15 and I have set it as a datetime object.

I set the datetime column as t

2条回答
  •  旧时难觅i
    2020-12-10 09:02

    This is a split per year.

    import pandas as pd
    import dateutil.parser
    dfile = 'rg_unificado.csv'
    df = pd.read_csv(dfile, sep='|', quotechar='"', encoding='latin-1')
    df['FECHA'] = df['FECHA'].apply(lambda x: dateutil.parser.parse(x)) 
    #http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
    #use to_period
    per = df['FECHA'].dt.to_period("Y")
    #group by that period
    agg = df.groupby([per])
    for year, group in agg:
        #this simple save the data
        datep =  str(year).replace('-', '')
        filename = '%s_%s.csv' % (dfile.replace('.csv', ''), datep)
        group.to_csv(filename, sep='|', quotechar='"', encoding='latin-1', index=False, header=True)
    

提交回复
热议问题