0

I have created a for loop which extracts all my files from my path and prints the same. I wanted to put this entire functions output into a dataframe and write it to a csv. I know we can do this in R by creating a vector but how do we do it in python?

import pandas
import glob

for filepath in glob.iglob('*.csv'):
    fl = pandas.read_csv(filepath);
    fl.insert(25,'city',filepath)
    print(fl.groupby('city').mean())
martineau
  • 119,623
  • 25
  • 170
  • 301
NewInPython
  • 247
  • 1
  • 5
  • 13
  • 1
    Related (possible duplicate): [Import multiple csv files into pandas and concatenate into one DataFrame](https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe) – wwii Jul 31 '19 at 18:05
  • fl is already a dataframe. Here's the documentation to write a dataframe to a CSV: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html – rajah9 Jul 31 '19 at 18:51

1 Answers1

0

Your f1 variable is already a DataFrame. To output it into a csv file on the same directory just use the to_csv methond from pandas.DataFrame.

f1.to_csv('filename.csv')
Joe
  • 879
  • 2
  • 6
  • 15