Reputation: 113
I have a list of the dataframes as follows:
dept = [Phy,Chem,Bio,Maths,Social]
Each of the dataframes have huge amount of data. I need to access the name of these dataframes from the list for further use in looping. I tried this code for example:
str(dept[0])
But this is giving me the contents of the dataframe 'Phy'. Where as I need just to print 'Phy'.
Upvotes: 3
Views: 953
Reputation: 6665
What follows only makes sense if you do not have the control in the first place on how the dataframes are created (cf. the last section of the answer for a common-sense suggestion).
get_name_of
, a custom function to do this job
def get_name_of(o):
o_id = id(o)
for objname, obj in globals().items():
obj_id = id(obj)
if obj_id == o_id:
return objname
And then use it as follows
>>> get_name_of(dept[0])
'Phy'
your_dict = {}
# ...
your_dict['Chem'] = pd.DataFrame(...)
# and so on...
Upvotes: 3