Home > OS >  exporting DF to csv with variable file names
exporting DF to csv with variable file names

Time:08-14

I would like to keep this same code but adjust the .to_csv section to save a variable repeating file name

for i in range(round(len(usersDf)/577)):
usersDf.loc[i*577:(i 1)*577,:].to_csv('Stored_files_' str(i) '.csv')

I would like the names of the exported files to look like this...I think I need some sort of loop?

Period 1 layer 1.csv

Period 1 layer 2.csv

Period 1 layer 3.csv

Period 1 layer 4.csv

Period 1 layer 5.csv

Period 2 layer 1.csv

Period 2 layer 2.csv

Period 2 layer 3.csv

Period 2 layer 4.csv

Period 2 layer 5.csv

Period 3 layer 1.csv

And so on....till the loop ends

CodePudding user response:

no_of_periods = 577
    no_of_layers = 5
    for i in range(no_of_periods):
        for j in range(no_of_layers):
            filename = f"Period {i 1} layer {j 1}.csv"
            #print(filename)
            usersDf.loc[i*577:(i 1)*577,:].to_csv(filename)

You can try this one out.

  • Related