MaMo
MaMo

Reputation: 585

Python: Add value of iteration variable to variable name within for loop

I have a pandas df with a column (let's say col3) containing a number. These numbers are used in multiple rows and I want to run a function for rows of each number separatly.

So I wrote each number once into an array like this:

l = df.col3.unique()

Then a for loop is used to run a function for each number:

for i in l:
   a,b = func(df[df.col3 == i])

So the function gets rows where col3 contains the value of i of each run. The function returns two data frames (a and b in this case). I need these two returned data frames of each run.

I want to be able to identify them properly. For that I would like to save returned data frames within the loop like this:

First run: a123, b123 Second run a456, b456 Third run: a789, b789

Means the name of the dataframe contains the current value of i.

I already read I should not use global variables for dynamic variable names but do not know how to realize this instead.

Thank you :)

Upvotes: 0

Views: 3400

Answers (1)

Lambda
Lambda

Reputation: 1392

Solution A (recommended):

dfs = {}

for i in l:
    dfs["a"+str(i)], dfs["b"+str(i)] = func(df[df.col3 == i])
...

And then you can use the dataframes like this:

func2(dfs["a1"]) # dfs["a1"] represents func(df[df.col3 == i])'s first return.
...

Solution B (not recommended)

If you absolutely want to use local variables, you need:

for i in l:
    locals()["a"+str(i)], locals()["b"+str(i)] = func(df[df.col3 == i])

And then you can use the dataframes with their variable names a1,b1 etc.

Upvotes: 1

Related Questions