0

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)

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

It seems to work, however i get this error: [Errno 22] Invalid argument:' Should have something to do with my filepath, but can't figure out how to solve it
so you might need to change your code to be this variable = 'Documents' path = rf'C:\Users\Computer\{variable}\YourFile.csv' because the f string will allow you to insert variables into a string function and the r will allow you to have a raw file. r strings are usually used when manipulating file paths. This is because it makes it where you don't have to do \\ everytime you need to do a \ in your code
It still doesn't work.. i'll try to add the variable a dataframe. If i'm correct pandas dataframes can have more than 2 dimensions... Afther this i will continue processing the data, so no need to save csv files allready

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.