ORIGINAL QUESTION:
I'm writing a while loop to loop over certain columns. In this while loop I want to create a variable of which the name partly consists of the column name it is looping over.
x=2
length=len(grouped_class.columns)
while x<length:
x=x+1
(grouped_class.columns[x])_largest = x+5
...
This is my current code (=x+5 is not actual code, but as example), but it returns a syntax error. If I run grouped_class.columns[x] in the shell it returns the name of that column, for example : "ColumnA". I want to use this "ColumnA" as first part of a variable name.
So in the variable list it would return: ColumnA_largest
In this way I can store the result for each column in a seperate variable.
How can I do this?
EDIT: QUESTION GENERALIZED
How can I use a string obtained by df.column[x] as input for a variable name?
Example df:
ColumnA ColumnB ColumnC
5 6 4
6 10 2
If I run df.columns[1] it returns "ColumnB"
I want to use this "ColumnB" as part of the name when assigning a variable.
Imagine I want to create the variable COLNAME_sum = x + 5
I would like to change the COLNAME to the string I obtained from df.columns[1] (="ColumnB")
Expected output:
A variable named ColumnB_sum.
How can I do this?