Home > Software engineering >  Writing csvs from dataframes in a list
Writing csvs from dataframes in a list

Time:12-28

I have a dataframe with a picture below, it contains a list of dataframes in the 2nd column, the column name is content. I also have a column called racenames in the 3rd column that I'd like to put inside of my csvs while running through the code. I can't figure out a way to get the list of dataframes to write to a csv in a loop or anything.

The code below works at writing a csv for the first dataframe in the content column, but I would like to write all of the dataframes at the same time so that I don't need to manually change the numbers/names for hours. All of the data has been scraped in one of my prior loops.

write.csv(ARCA_separate[[2]][[1]], file = "C:\\Users\\bubba\\Desktop\\RStuff\\Scraping\\ARCA 2012 Season\\*racename*.csv")

Here is what the data I'm working with looks like. The dataframe is called ARCA_separate.

enter image description here

How do I write all of the csvs and grab the corresponding racename in the same row to put into my csv name?

CodePudding user response:

You can try this:

purrr::walk2(ARCA_separate$content, ARCA_separate$racename, function (x, y) write_csv(x, paste0("C:\\Users\\bubba\\Desktop\\RStuff\\Scraping\\ARCA 2012 Season\\", y, ".csv")))

CodePudding user response:

So you can just iterate using iterrows method

for index, row in ARCA_separate.iterrows():
    your_path = ""  # Put your target path here
    row['content'].to_csv(os.path.join(your_path, f"{row['racename']}.csv"), index=False)
  • Related