-3

I would like to retrieve the name of my pandas. For instance:

A=pd.DataFrame()
print('the name of my pandas is ' + XXX)

What should be XXX to get

the name of my pandas is A 
olivier dadoun
  • 622
  • 6
  • 22
  • Does this answer your question? [Get the name of a pandas DataFrame](https://stackoverflow.com/questions/31727333/get-the-name-of-a-pandas-dataframe) – Marcus Vinicius Pompeu Jun 11 '21 at 16:18
  • you will find the answer here: https://stackoverflow.com/a/50620134/206413 – Marcus Vinicius Pompeu Jun 11 '21 at 16:19
  • Does this answer your question? [How can you print a variable name in python?](https://stackoverflow.com/questions/592746/how-can-you-print-a-variable-name-in-python) – Alex Jun 11 '21 at 16:20
  • 1
    if you assign `XXX = A` then it will have different name. But variable's name is not assigned to `DataFrame` (or any other data) and you can't get `A` when you have assigned to `XXX`. And if you put `print( ... + A)` then you already know name and you can put it manually in text `"...is A"`. For me all this is only waste of time. – furas Jun 11 '21 at 16:31
  • 1
    Why, exactly, do you want to do this? – MattDMo Jun 11 '21 at 18:15
  • I would to implement a method which takes 2 arguments: a list of pandas and a list of columns (associates to those pandas). Basically this method merge all the pandas columns into a 'master' pandas. I would like to rename the pandas columns using the name of the associate pandas (cause sometimes the columns names are the same). To do so I need to retrieve the name of the considered pandas. Does it makes sense ? – olivier dadoun Jun 11 '21 at 20:11

2 Answers2

3

As others have mentioned, this is more of a Python question. But over time I've realized that the simplest way to keep track of names of dataframe (and variables in general) is to just store them in a dictionary.

dfs_stored = {}

# do some procedure
for varname in varname_list:
    dfs_stored[varname] = pd.DataFrame(some_data)

dfs_stored['A'] # lookup by name

xyzjayne
  • 1,331
  • 9
  • 25
2

I think this question is more about python than pandas. You can see how to view all defined variables in python on Geeks for Geeks.

This snippet can help you

A=pd.DataFrame()

all_variables = dir()
for name in all_variables:
    if type(eval(name)) == pd.core.frame.DataFrame:
        print('the name of my pandas is ' + name)
Matin Zivdar
  • 593
  • 5
  • 20