Get the name of a pandas DataFrame
Solution 1
You can name the dataframe with the following, and then call the name wherever you like:
import pandas as pd
df = pd.DataFrame( data=np.ones([4,4]) )
df.name = 'Ones'
print df.name
>>>
Ones
Hope that helps.
Solution 2
Sometimes df.name
doesn't work.
you might get an error message:
'DataFrame' object has no attribute 'name'
try the below function:
def get_df_name(df):
name =[x for x in globals() if globals()[x] is df][0]
return name
Solution 3
In many situations, a custom attribute attached to a pd.DataFrame
object is not necessary. In addition, note that pandas
-object attributes may not serialize. So pickling will lose this data.
Instead, consider creating a dictionary with appropriately named keys and access the dataframe via dfs['some_label']
.
df = pd.DataFrame()
dfs = {'some_label': df}
Solution 4
From here what I understand DataFrames are:
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects.
And Series are:
Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.).
Series have a name
attribute which can be accessed like so:
In [27]: s = pd.Series(np.random.randn(5), name='something')
In [28]: s
Out[28]:
0 0.541
1 -1.175
2 0.129
3 0.043
4 -0.429
Name: something, dtype: float64
In [29]: s.name
Out[29]: 'something'
EDIT: Based on OP's comments, I think OP was looking for something like:
>>> df = pd.DataFrame(...)
>>> df.name = 'df' # making a custom attribute that DataFrame doesn't intrinsically have
>>> print(df.name)
'df'
Solution 5
Here is a sample function: 'df.name = file` : Sixth line in the code below
def df_list():
filename_list = current_stage_files(PATH)
df_list = []
for file in filename_list:
df = pd.read_csv(PATH+file)
df.name = file
df_list.append(df)
return df_list
Related videos on Youtube
leo
Updated on April 22, 2022Comments
-
leo about 2 years
How do I get the name of a DataFrame and print it as a string?
Example:
boston
(var name assigned to a csv file)import pandas as pd boston = pd.read_csv('boston.csv') print('The winner is team A based on the %s table.) % boston
-
leo almost 9 yearsi need the name to be a variable somewhat like name=<table name>
-
leo almost 9 yearsI need to have the name as a variable. import pandas as pd df = pd.DataFrame( data=np.ones([4,4]) ) df.name = 'df' print df.name >>> df
-
aznbanana9 almost 9 yearsWhat do you mean variable? Like calling
df
prints the name"df"
instead of printing the dataframe? -
leo almost 9 yearsYes. That's what I meant.
-
aznbanana9 almost 9 yearsBut how do you want the data frame to be called?
-
leo almost 9 yearssay the name of the file is apple.csv. I want it to get printed like The file came from apple. --- only that apple has to be dynamic depending on the name of the csv file.
-
tmthydvnprt over 7 yearsFor posterity, as of v 0.18.1 this does not survive pickling (for v 0.18.1 use
to_pickle
/read_pickle
instead ofsave
/load
if trying to reproduce the GitHub comment). -
tmthydvnprt over 7 yearsA workaround I found is to place your
DataFrame
's name in the index's name attribute (e.g.df.index.name = 'Ones'
). This is maintained during pickling. This only works if yourDataFrame
's index is not already named something useful... -
tmthydvnprt over 7 yearsFYI, this was found while using
DataFrame
s insidemultiprocessing.Pool()
workers. The attributes were not maintained during.map()
because of the pickling it uses. -
IndigoChild about 6 years@leo, any solution to this? did you get the dataframe name without the quotes?
-
sapo_cosmico almost 6 yearsThis is a poor idea because if you as much as
drop
something, the returned object will no longer have aname
attribute. It's tempting, but will create inexplicable errors down the line. -
Mohamed Thasin ah over 5 yearsIt will throw ` 'DataFrame' object has no attribute 'name'` when it doesn't assign any name
-
Admin about 5 yearsReally veru bad idea. If you call df.name = Ones is the same than df['name] = 'Ones'. it means the valiues for that column will be 'One'. SO it is not a correct answer. You can stor your dataframes within a dictionary and use the key to identify them
-
Zecong Hu over 3 yearsJust to make sure people aren't confused: what the snippet here does is to find the dataframe in all currently defined global variables and return its variable name. This is NOT guaranteed to work (e.g. your DF is a local variable) and there are no error handling mechanisms in place. You should only use this if you're sure what you're doing!