0

I am creating pandas DataFrames in a for loop and I would like to save them in csv files with different names at each iteration of the for loop.

I know how to save one DataFrame:

path = r"C:\Users\SessionName\FolderName\FileName.csv"
df.to_csv(path)

Now when I have a list of strings, e.g.

countries = ['United States', 'China', 'Russia', 'India']

I would like the four files to be named United States_ranking.csv, China_ranking.csv, etc.

I have tried:

for country in countries:
    path = r"C:\Users\SessionName\FolderName\" + country + "_ranking.csv"

But is won't work.

4
  • do you want the csvs to be exactly the same ? is there a country column you can group on first ? Commented Oct 22, 2020 at 9:48
  • 1
    Do you have the "df.to_cvs(path)" line inside your for-loop? Commented Oct 22, 2020 at 9:54
  • 1
    ...and lower-case 'f' in "for". Commented Oct 22, 2020 at 9:55
  • @Carl yes I do! Corrected the 'F' Commented Oct 22, 2020 at 10:03

5 Answers 5

2

use this:

for country in countries:
    path = r"C:\\Users\\SessionName\\FolderName\\ {} _ranking.csv".format(country)
Sign up to request clarification or add additional context in comments.

Comments

2

personally, I would use pathlib to handle your paths. also be careful for typos in your code.

for example,

import pandas as pd
from pathlib import Path

src_path  = r"C:\Users\SessionName\FolderName\FileName.csv"
countries = ['United States', 'China', 'Russia', 'India']

for country in countries:
    p = Path(src_path).parent.joinpath(f"{country}_ranking.csv")
    df.to_csv(p,index=False)

this will write:

C:\Users\SessionName\FolderName\United States_ranking.csv
C:\Users\SessionName\FolderName\China_ranking.csv
C:\Users\SessionName\FolderName\Russia_ranking.csv
C:\Users\SessionName\FolderName\India_ranking.csv

The benefit of having a pathlib object here is that you can check if the directory is valid or if the file exists before hand.

print(p)
WindowsPath('C:/Users/SessionName/FolderName/United States_ranking.csv')
if not p.is_file():
   df.to_csv(p,index=False)
else:
    print('file exists')

1 Comment

@jezrael I think so too, but OP hasn't shown the structure of CSV so don't want to assume
0

You have to write the path like this, otherwise it causes a problem with "\"

for country in countries:
    path = r"C:\\Users\\SessionName\\FolderName\\" + country + "_ranking.csv"

2 Comments

He/she uses a raw string, so thats not it.
No, the raw string doesn't work for strings which end with one '\'.
0

I prefer os.path.join(folder, country+filename)

for country in countries:
    path = os.path.join("C:\\Users\\SessionName\\FolderName\\", country + "_ranking.csv")
    df.loc[df.country == country].to_csv(path)

Comments

0

Looks like rawstring dont work with "\" and the end of the string.

You can fix it by adding extra \ after your path to folder by puting double "\\" in another string.

countries = ['United States', 'China', 'Russia', 'India']
for country in countries:
    path = r"C:\Users\SessionName\FolderName" + "\\" + country + "_ranking.csv"

Comments

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.