LostinSpatialAnalysis
LostinSpatialAnalysis

Reputation: 641

How to make output file name reflect for loop list in python?

I have a function that does an action for every item in a list and then outputs a final product file for each item in that list. What I am trying to do is append a string to each of the output files that reflects the items in the initial list. I will explain more in detail here.

I have the following code:

list = ['Blue', 'Red', 'Green']
df = pd.read_csv('data.csv')

for i in list:
    df_new = (df[i] * 2)
    df_new.to_csv('Path/to/My/Folder/Name.csv')

Ok, so what is going on here is that I have a file 'data.csv' that has 3 columns with unique values, the 'Blue' column, the 'Red' column, and the 'Green' column. From all of this code, I want to produce 3 separate output .csv files, one file where the 'Blue' column is multiplied by 2, the next where the 'Red' column is multiplied by 2, and lastly a file where the 'Green' column is multiplied by 2. So what My code does is first write a list of all the column names, then open the .csv file as a dataframe, and then FOR EACH color in the list, multiply that column by 2, and send each product to a new dataframe.

What I am confused about is how to go about naming the output .csv files so I know which is which, specifically which column was multiplied by 2. Specifically I simply want my files named as such: 'Name_Blue.csv', 'Name_Red.csv', and 'Name_Green.csv'. How can I do this so that it works with my for loop code? I am not sure what this iterative naming process would even be called.

Upvotes: 1

Views: 2752

Answers (1)

Cameron
Cameron

Reputation: 377

You need to use a formatted string (A string with an f at the beginning). For example:

name = "foo"
greeting = f'Hello, {name}!'

Inside those curly brackets is the variable you want to put in the string. So here's the modified code:

colors = ['Blue', 'Red', 'Green']
df = pd.read_csv('data.csv')

for i in colors:
    df_new = (df[i] * 2)
    df_new.to_csv(f'Path/to/My/Folder/Name_{i}.csv')

Now the formatted string in the last line will input i (the item in the list) as the name of the file!

Upvotes: 1

Related Questions