Home > Enterprise >  Selecting the items from a list of data frames by their names
Selecting the items from a list of data frames by their names

Time:12-10

i have a list of data frames list = [df1, df2, df3, df4]. I am trying to to reference these data frame by their names only. for example, I want to use the string 'df1' as input and then use it to get the data frame df1 from the list. so i thought of trying this:

idx = list(eval('df1'))
df1 = list[idx]

The problem is the above code returns the index of the first item only, df1, however for the rest, I get the error: ValueError: Can only compare identically-labeled DataFrame objects.

Not sure what is going wrong here.
Any help?
Thanks

CodePudding user response:

You can create a dict comprehension that calls a function to create a dictionary, finally use .get() to retrieve later.

from typing import Any

import pandas as pd


def stringify_variable_name(var: Any) -> str:
    globals_dict = globals()

    return [x for x in globals_dict if globals_dict.get(x) is var][0]


df1 = pd.DataFrame({"spam": [1, 2, 3]})
df2 = pd.DataFrame({"ham": [4, 5, 6]})
df3 = pd.DataFrame({"eggs": [7, 8, 9]})
df4 = pd.DataFrame({"sausage": [10, 11, 12]})

dfs = [df1, df2, df3, df4]
df_mapping = {stringify_variable_name(x): x for x in dfs}

df = df_mapping.get("df2")
print(df)

Output:

   ham
0    4
1    5
2    6
  • Related