Cornelis
Cornelis

Reputation: 435

While loop to save csv files with conditional name

I'd like to make a while / for loop in python to save dataframes into csv files.

The files should be named something like: Day1, Day2, Day3...Day90

First i determine for the n-th day, how large of a sample I should draw, based on a forecast.

Then the sample is drawn from the population (Orders_df), and should be written into a csv file

Period = list(range(0,90))
Period

for item in Period:
Percentage = (forecast['yhat'].iloc[Period]) / (forecast['yhat'].max())
Samplesize = round( Orders_df.lat.count() / 90 * Percentage)
Orders_df.sample(Samplesize).to_csv('Day'&Period&'.csv', index=False)

Upvotes: 0

Views: 57

Answers (1)

ArchAngelPwn
ArchAngelPwn

Reputation: 3046

IIUC you could do something to this effect

for item in Period:
     Orders_df.sample(Samplesize).to_csv(f'{item}<whatever you wanna call the file>.csv', index = False)

Upvotes: 1

Related Questions