在 Pandas 中将 DataFrame 名称保存为 .csv 文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25333044/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:22:37  来源:igfitidea点击:

Saving DataFrame names as .csv file names in Pandas

pythonpandas

提问by verbsintransit

In [37]: blue = pd.DataFrame({'A': ['foo','foo','foo','bar','bar'], 'B': [4.0, 4.0, 5.0, 8.0, 8.0]})

In [38]: blue
Out[38]: 
     A  B
0  foo  4
1  foo  4
2  foo  5
3  bar  8
4  bar  8

In [39]: red = pd.DataFrame({'A': ['foo','foo','foo','bar','bar'], 'B': [np.nan, np.nan, np.nan, np.nan, np.nan]})

In [40]: red
Out[40]: 
     A   B
0  foo NaN
1  foo NaN
2  foo NaN
3  bar NaN
4  bar NaN

In [41]: for df in [blue, red]:
   ....:     df.to_csv(str(df))
   ....:     

In [42]: !ls
     A  B?0  foo  4?1  foo  4?2  foo  5?3  bar  8?4  bar  8       A   B?0  foo NaN?1  foo NaN?2  foo NaN?3  bar NaN?4  bar NaN  postinstall.sh  vagrant

I have some DataFrames. I loop over each DataFrame to work on them. At the end of the loop I want to save each DataFrame as a .csv file named after the DataFrame. I know that it's generally difficult to stringify the name of a variable in Python, but I have to think that I'm missing something obvious here. There is no "name" attribute for DataFrames, so what do I do?

我有一些数据帧。我遍历每个 DataFrame 来处理它们。在循环结束时,我想将每个 DataFrame 保存为一个以 DataFrame 命名的 .csv 文件。我知道在 Python 中字符串化变量的名称通常很困难,但我不得不认为我在这里遗漏了一些明显的东西。DataFrames 没有“名称”属性,我该怎么办?

回答by EdChum

You can just add an attribute to the df, same as any other python object that has a __dict__attributeand use it later:

您可以只向 df 添加一个属性,就像任何其他具有__dict__属性的Python 对象一样,稍后使用它:

In [2]:

blue.name = 'blue'
red.name = 'red'
df_list = [blue, red]
for df in df_list:
    print(df.name)
    df.to_csv(df.name + '.csv')
blue
red

Even better, for convenience you can store the csv name and use it later too:

更好的是,为了方便起见,您可以存储 csv 名称并在以后使用它:

In [5]:

blue.name = 'blue'
blue.csv_path = 'blue.csv'
red.name = 'red'
red.csv_path = 'red.csv'
df_list = [blue, red]
for df in df_list:
    print(df.name)
    print(df.csv_path)
    df.to_csv(df.csv_path)
blue
blue.csv
red
red.csv

EDITAs @Jeff has pointed out, the attributes will not persist across most operations on the df as a copy of the df is returned and these attributes are not copied across so be aware of this.

编辑正如@Jeff 指出的那样,属性不会在 df 上的大多数操作中持续存在,因为返回了 df 的副本,并且不会复制这些属性,因此请注意这一点。