Home > Software design >  How to save each row to csv in dataframe AND name the file based on the the first column in each row
How to save each row to csv in dataframe AND name the file based on the the first column in each row

Time:04-12

I have the following df, with the row 0 being the header:

teacher,grade,subject

black,a,english

grayson,b,math

yodd,a,science

What is the best way to use export_csv in python to save each row to a csv so that the files are named:

black.csv

grayson.csv

yodd.csv

Contents of black.csv will be:

teacher,grade,subject

black,a,english

Thanks in advance!

CodePudding user response:

This can be done simply by using pandas:

import pandas as pd

df = pd.read_csv('your_data.csv', header=1)

df.set_index('teacher', inplace=True)

for teacher, data in df.iterrows():
    data.to_csv(teacher   '.csv')
  • Related