1

So I have a list of Dataframe like :

dfs = [peru,euro,usa]   <---all the elements are dataframe

I want to get the name of the each df as a string. Like:

for i in dfs:
  print(str(i))
#Expected Output:
'peru'
'euro'
'usa'

But I am getting the entire dataframe contents for peru,eur and usa as string.

Any clue?

pythondumb
  • 1,187
  • 1
  • 15
  • 30
  • 1
    Unfortunately DataFrames' do not have a "name" attribute or equivalent by default. You'd need to set it yourself manually or name the index. See https://stackoverflow.com/questions/31727333/get-the-name-of-a-pandas-dataframe for some workarounds. – Chris Adams Apr 22 '21 at 14:58
  • 1
    are we sure this isn't a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? why do you want this? – anky Apr 22 '21 at 15:06

1 Answers1

0

when you attach the frames into a list, you aren't attaching the variables, but the content as you were getting as an output. in order to do that, before you have to give the dataframe names:

peru.name = 'peru'
euro.name = 'euro'
usa.name = 'usa'

then, you could throw them into a list using this property:

dfs = [peru.name, euro.name, usa.name]