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.
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)