Ison
Ison

Reputation: 403

Convert list into separate dataframes

I had to group my df into periods.

result = [g for n, g in df.groupby(pd.Grouper(key='Date',freq='M'))]

As a result of the Grouper i got the list with the following structure:

[   timestamp        Address            Problem      Val  
 205 2018-09-01   Malindi              Men at work    1  
 206 2018-09-08   Parkside             Men at work    1  
 207 2018-09-12   Parkside             Men at work    1  
 208 2018-09-26   Dawsonville          Rodent         1,
    timestamp        Address            Problem      Val
 220 2018-10-01   Malindi              Men at work    1  
 221 2018-10-05   Parkside             Men at work    0  
 222 2018-10-16   Parkside             Men at work    0  
 223 2018-10-28   Dawsonville          Rodent         0]

My goal is to save each period to separate df and csv file. But I can't get how to convert this list into dataframes keeping the structure. All I get is df with two rows. And If I want to split list into elements I got the error that the dataframe has no attribute split.

WHat I need to do to get the desired dataframes from the list?
Thanks in advance for the help

Upvotes: 1

Views: 192

Answers (1)

Rakesh
Rakesh

Reputation: 82765

If you need each group in a separate CSV file

Use:

for i, (n, g) in enumerate(df.groupby(pd.Grouper(key='Date',freq='M'))):
    g.to_csv("filename_{}.csv".format(i)

Upvotes: 1

Related Questions