arpit joshi
arpit joshi

Reputation: 2134

Group by Column in Dataframe and create seperate csv for each group

I have a huge csv file of 1 GB which contains records of each day .Example like below

Date        orderquantity

2015-06-19   23
2015-06-19   30
2015-06-20   33
2015-06-20   40

So record is present of each and everyday ,is there a efficient way in Python Pandas data frame where I can group the data according to date and then store it as a seperate csv for each date .

My output result for above example would be

  CSV 1 
Date           orderquantity
2015-06-19      23
2015-06-19      30

CSV 2
 Date           orderquantity
 2015-06-20     33
 2015-06-20     40

Will I have to like sort/group by date in the data frame and then have a for loop and iterate through the entire data frame ?

Upvotes: 2

Views: 1949

Answers (1)

piRSquared
piRSquared

Reputation: 294258

Try this:

for name, group in df.groupby('Date'):
    group.to_csv('{}.csv'.format(name), index=False)

Upvotes: 3

Related Questions