Get the name of a pandas DataFrame
如何获取数据帧的名称并将其打印为字符串?
例子:
1 2 3 | boston = read_csv('boston.csv') print ('The winner is team A based on the %s table.) % boston |
您可以使用以下名称命名数据帧,然后在任何需要的地方调用该名称:
1 2 3 4 5 6 7 | import pandas as pd df = pd.DataFrame( data=np.ones([4,4]) ) df.name = 'Ones' print df.name >>> Ones |
希望有帮助。
有时
您可能会收到一条错误消息:
'DataFrame' object has no attribute 'name'
号
尝试以下功能:
1 2 3 | def get_df_name(df): name =[x for x in globals() if globals()[x] is df][0] return name |
。
在许多情况下,不需要附加到
相反,考虑使用适当命名的键创建字典,并通过
1 2 3 | df = pd.DataFrame() dfs = {'some_label': df} |
从这里我了解到的数据帧是:
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.
号
系列包括:
Series is a one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, Python objects, etc.).
号
系列有一个
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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' |
号
编辑:根据Op的评论,我认为Op正在寻找类似的东西:
1 2 3 4 | >>> df = pd.DataFrame(...) >>> df.name = 'df' # making a custom attribute that DataFrame doesn't intrinsically have >>> print(df.name) 'df' |