Home > Blockchain >  Exporting a dataframe to several csv files based on value in a column in python
Exporting a dataframe to several csv files based on value in a column in python

Time:12-29

I have the following code for exporting each "id" to a separate csv file. Each csv file should be named as an id. Here in my case, we should have three different csv files named 23, 24, and 25. I have the code in below but it gives me an error. Thanks.

import pandas as pd

path = "users/AR/csv files"

for (id), id in df.groupby(['id']):
     group.to_csv(f'{id}.csv', directory=path, index=False)
NameError: name 'group' is not defined
id date count
23 2/2/2016 24
24 2/4/2016 56
25 2/3/2016 135
23 3/4/2016 46
24 3/8/2016 176
25 3/9/2016 23
23 3/16/2016 98
24 3/13/2016 114
25 3/17/2016 43

I am expecting three separated csv files in the directory.

CodePudding user response:

Just change:

for (id), id in df.groupby(['id']):

with:

for id, group in df.groupby(['id']):

When iterating groupby object, you get separate grouping value (in id, in your case), from particular group sub-dataframe (here: in group). id is then used to name a csv file, while group df is exported to csv.

Furthermore, there is no directory parameter in to_csv method. The easiest method is to include path in the filename string, like f'users/AR/csv files/{id}.csv'. If you need OS-agnostic solution, you can use os.path method.

CodePudding user response:

This can be done with list comps:

dfs = [x.reset_index(drop=True) for _, x in df.groupby("id")]
[x.to_csv(f"users/AR/csv files/{x['id'][0]}.csv", index=False) for x in dfs]
  • Related