2

I have a list of dataframe variables:

 B = [df1,df2,df3,df4]

I am running a for loop like below:

 for element in B:
     element + '_norm' = (element - element.mean())/ (element.max() - 
     element.min())

How do I write the variable name so that I may reference the normalized dataframe? That is to say I would like to be able to define the normalized dataframe 3 using the variable "df3_norm". How can I write the variable into the for loop so that the "df3_norm" format can be referenced?

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
SBF12345
  • 75
  • 1
  • 2
  • 10

1 Answers1

0

Use dict:

df_dict = {}
for i, element in enumerate(B):
    df_dict['df%i_norm' % i] = (element - element.mean())/(element.max() - element.min())

BTW, normalization involves subtracting min from elements not mean.

If you want normalization:

df_dict['df%i_norm' % i] = (element - element.min())/(element.max() - element.min())
#                                         ^^^^^

Else if you want standardization:

df_dict['df%i_norm' % i] = (element - element.mean())/(element.std())
Chris
  • 29,127
  • 3
  • 28
  • 51
  • `'df%i_norm'` doesn't inject the variable `i` into the key, you'd need `f"df{i}_norm"` – Paul H Mar 01 '19 at 02:01
  • I've tried to run the code as: df_dict = {df1, df2, df3, df4} – SBF12345 Mar 01 '19 at 15:48
  • when I run the code and try to print df1_norm I receive an error on the df_dict = {} line. The error reads "series objects are mutable, thus they cannot be hashed" I don't understand what is meant by 'hashing' of the series object 'df_dict'? – SBF12345 Mar 01 '19 at 15:57
  • With the above error it sounds like the dict = {df1,df2...} doesn't allow for the hashed integer to be accepted as such, because the elements of the dictionary are mutable. The dictionary values are initially parsed from excel file's and named as df1, df2 etc. if I am understanding the conflict correctly the dilemma is in the structure of the dictionary. I need to reference existing variables for the looping procedure, and rename the output variables to correspond to input variables. Any ideas? – SBF12345 Mar 02 '19 at 17:14