1

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.

2
  • 1
    The term you want is "string concatenation", i.e. gluing two strings together. In python it's as easy as "hello" + "world", or in you case "Name_" + i + ".csv". You can also look at the string format options which give you fancier ways to create strings. Commented Jan 4, 2022 at 20:50
  • Take a look at f-strings. Also, avoid using list as a variable name. You are overwriting the list class. Commented Jan 4, 2022 at 20:54

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Avoid using list as a variable name. You are overwriting the list class.
It's worth highlighting that f strings are only available since python 3.6.

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.