Python 获取 Pandas DataFrame 的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31727333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the name of a pandas DataFrame
提问by leo
How do I get the name of a DataFrame and print it as a string?
如何获取 DataFrame 的名称并将其打印为字符串?
Example:
例子:
boston
(var name assigned to a csv file)
boston
(分配给 csv 文件的 var 名称)
import pandas as pd
boston = pd.read_csv('boston.csv')
print('The winner is team A based on the %s table.) % boston
回答by ajsp
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.
希望有帮助。
回答by aznbanana9
From herewhat I understand DataFrames are:
从这里我理解的 DataFrames 是:
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.
DataFrame 是一种二维标记数据结构,具有可能不同类型的列。您可以将其视为电子表格或 SQL 表,或 Series 对象的字典。
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 是一个一维标记数组,能够保存任何数据类型(整数、字符串、浮点数、Python 对象等)。
Series have a name
attribute which can be accessed like so:
系列有一个name
可以像这样访问的属性:
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:
编辑:根据 OP 的评论,我认为 OP 正在寻找类似的东西:
>>> df = pd.DataFrame(...)
>>> df.name = 'df' # making a custom attribute that DataFrame doesn't intrinsically have
>>> print(df.name)
'df'
回答by Min
Sometimes df.name
doesn't work.
有时df.name
不起作用。
you might get an error message:
您可能会收到一条错误消息:
'DataFrame' object has no attribute 'name'
'DataFrame' 对象没有属性 'name'
try the below function:
试试下面的功能:
def get_df_name(df):
name =[x for x in globals() if globals()[x] is df][0]
return name
回答by jpp
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.
在许多情况下,附加到pd.DataFrame
对象的自定义属性不是必需的。此外,请注意pandas
-object 属性可能不会序列化。所以酸洗会丢失这些数据。
Instead, consider creating a dictionary with appropriately named keys and access the dataframe via dfs['some_label']
.
相反,请考虑使用适当命名的键创建字典并通过dfs['some_label']
.
df = pd.DataFrame()
dfs = {'some_label': df}
回答by Arjjun
Here is a sample function: 'df.name = file` : Sixth line in the code below
这是一个示例函数: 'df.name = file` :下面代码中的第六行
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
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