1

This should be a pretty simple question, but I'm looking to programmatically insert the name of a pandas DataFrame into that DataFrame's column names.

Say I have the following DataFrame:

name_of_df = pandas.DataFrame({1: ['a','b','c','d'], 2: [1,2,3,4]})
print name_of_df
        1  2
     0  a  1
     1  b  2
     2  c  3
     3  d  4

I want to have following:

name_of_df = %%some_function%%(name_of_df)

print name_of_df
           name_of_df1  name_of_df2
        0  a            1
        1  b            2
        2  c            3
        3  d            4

..where, as you can see, the name of the DataFrame is programatically inputted into the column names. I know pandas DataFrames don't have a __name__ attribute, so I'm drawing a blank on how to do this.

Please note that I want to do this programatically, so altering the names of the columns with a hardcoded 'name_of_df' string won't work.

Bryan
  • 5,999
  • 9
  • 29
  • 50
  • 1
    Very related question: [Convert Variable Name to String?](http://stackoverflow.com/questions/1534504/convert-variable-name-to-string). – WGS Nov 18 '14 at 16:25
  • This is actually surprisingly hard. Is it possible to keep track of your df names at the very beginning when they are created? Or by putting them in a list you call by reference number: `myList[2]` or somesuch? – JD Long Nov 18 '14 at 17:01
  • unfortunately, I won't be able to know the name of the `DataFrame` object beforehand, as this function will have multiple use cases. – Bryan Nov 18 '14 at 17:14
  • why do you want/need to do this? i can't see a use case... – acushner Nov 18 '14 at 19:54

1 Answers1

1

From the linked question, you can do something like this. Multiple names can point to the same DataFrame, so this will just grab the "first" one.

def first_name(obj):
   return [k for k in globals() if globals()[k] is obj and not k.startswith('_')][0]


In [24]: first_name(name_of_df)
Out[24]: 'name_of_df'
chrisb
  • 49,833
  • 8
  • 70
  • 70