0

I am attempting to get the dataframe names stored in the lists, iterating through 2 loops. Instead of getting the dataframe name itself like cat or sunday, I am getting the iterator name like df.

def get_df_name(df):
    name =[x for x in globals() if globals()[x] is df][0]
    return name

cat = pd.DataFrame()  
dog = pd.DataFrame()  
sunday = pd.DataFrame()  
monday = pd.DataFrame()  

for df in [cat, dog]:
    for df1 in [sunday, monday]:
            print('---------')
            print(get_df_name(df) + ' and ' + get_df_name(df1))

Currently my output is below


---------
df and df1
---------
df and df1
---------
df and df1
---------
df and df1

But I am expecting

---------
cat and sunday
---------
cat and monday
---------
dog and sunday
---------
dog and Monday
nerd
  • 473
  • 5
  • 15
  • 2
    Lists do not store names; they store values. Where the code says `for df in [cat, dog]:`, that first creates a new list object using **the DataFrames** that are currently named `cat` and `dog`, and then iterates over it. Those objects have *no knowledge of* the names `cat` or `dog`, or of those variables in the program. – Karl Knechtel Jun 20 '22 at 08:14
  • It seems like you want to work around this by looking for global variables that happen to name the DataFrame. Putting aside how fragile this is - did you [try to check](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) what happens when you run the code? For example, what do the results look like for `[x for x in globals() if globals()[x] is df]`? If you want to associate textual names with your DataFrames, better to use an actual data structure that makes this explicit. – Karl Knechtel Jun 20 '22 at 08:15
  • I cannot reproduce the results that you show. Please note that `globals` is a dictionary, and in older versions, the order of keys does not match insertion order whereas [in 3.6 and up it does](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6). But again I must emphasize that *it is a very bad idea to try to write code that relies on this*. – Karl Knechtel Jun 20 '22 at 08:20
  • 1
    If you care about a "name", you should store the dataframes in a dictionary: `dfs = { "cat": pd.DataFrame() }` – suvayu Jun 20 '22 at 08:53

1 Answers1

1
import pandas as pd
import inspect

def retrieve_name(var):
   callers_local_vars = inspect.currentframe().f_back.f_globals.items()
   return [var_name for var_name, var_val in callers_local_vars if var_val is var][0]

cat = pd.DataFrame()  
dog = pd.DataFrame()  
sunday = pd.DataFrame()  
monday = pd.DataFrame()
list1,list2 = [cat, dog],  [sunday, monday]
for x in range(len(list1)):
    for y in range(len(list2)):
        print('---------')
        print(retrieve_name(list1[x]) + ' and ' +retrieve_name(list2[y]) )

enter image description here

Mehul Gupta
  • 1,829
  • 3
  • 17
  • 33