Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python calling a list by name

Consider the case where I have three dataframes, that are called df1, df2 and df3.

df1 has 5 columns: x1, x2, x3, age, height.
df2 has 3 columns: x1, x2, weight.
df3 has 4 columns: x1, x2, x3, bmi.

For each of these dataframes, I want to create a list of the demographic variables e.g.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df1_demographics=['age', 'height']
df2_demographics=['weight']
df3_demographics=['bmi']

I want to be able to call the list in a loop in the following way:

for dataset in df1, df2, df3:

   print(dataset_demographics)
   

My actual loop is very long and I need to loop through the dataframes. That’s why I specifically want a way of calling the lists within a for loop looping through the dataframes.

The desired output of this loop would be

['age', 'height']
['weight']
['bmi']

>Solution :

I’m not entirely sure what your question is asking. Are you looking to associate a subset of columns with each of your dataframes like so?

demographic_cols = [
    ['age', 'height'],
    ['weight'],
    ['bmi']
]
dataframes = [df1, df2, df3]

for dataset, demographic_cols in zip(dataframes, demographic_cols):
    print(dataset, demographic_cols)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading