Search code examples
pythonexport-to-csvexport-to-excel

Is there a way to write & save several csv or excel files based on different names in a column of a dataframe?


I have a large DataFrame of about 2000 names that have other information and charts that go with their respective name. I would like to create a separate file (preferably an excel file, but csv works) for each name in the DataFrame.

df = pd.DataFrame({'name': ['Ben','Steve','Mary','Ben','Steve','Mary'], 'value': [1,2,3,4,5,6]})

This question is similar to what I'm looking to do, except it doesn't tell you how to call the function in a way that would save the csvs for each name. Writing multiple csv's from a function.

I've tried using that function and creating an outpath for it. I also tried something like this:

import os
g = df.groupby('name')

for n,g in df.groupby('name'):
 f_name = os.path.join('Desktop', str(n), 'Report.csv')
 g.to_csv(f_name)


def write_custom_csv(name):
   filtered = df[df['name'] == name]
   filtered.to_csv("Desktop\Report" + name + '.csv')
write_custom_csv(df)

I also tried with defining outpath:

outpath = "Desktop/Report"
def write_custom_csv(name):
    filtered = df[df['name'] == name]
    filtered.to_csv(outpath + name + '.csv')
write_custom_csv(df)

Expected output would be a separate csv file for each of Ben, Steve, Mary. Ben would have 1 and 4, Steve 2 and 5, and Mary 3 and 6 contained in the files. The name of the file would have their name in it like BenReport.csv.

Output is no excel file and this error message.

TypeError: Could not compare ['Desktop/Report'] with block values


Solution

  • The first way you tried should work. What's the error you're getting for that?

    The second way is close however you're passing the entire DataFrame when you're trying to pass just the names. Try instead:

    def write_custom_csv(df):
      for name in df['name'].unique():
        filtered = df[df['name'] == name]
        filtered.to_csv(os.path.join('Desktop', 'Report {}.csv'.format(name))
    
    write_custom_csv(df)