Python 熊猫:数据框 to_csv,如何设置列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51878601/
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
pandas: dataframe to_csv, how to set column names
提问by dudu
Code snippet:
代码片段:
import numpy as np
import pandas as pd
myseries=pd.Series(np.random.randn(5))
df=pd.DataFrame(myseries)
df.to_csv("output.csv")
Output:
输出:
0
0 0.51..
1 0.14..
2 -0.68..
3 0.48..
4 1.89..
I want the column name to be "values" instead of 0. How can I do that?
I suppose that I should replace the last statement with df.to_csv("output.csv", columns = ["values"]). But I got the keyerror:
u"None of [['values']] are in the [columns]"
I don't know what that means.
我希望列名是“值”而不是 0。我该怎么做?
我想我应该用 df.to_csv("output.csv", columns = ["values"]) 替换最后一个语句。但是我得到了关键错误:
u"None of [['values']] are in the [columns]"
我不知道这意味着什么。
[update]
Many answers say that I should use df.columns=['values']
. Well, that does not work for me. I concern not only what the dataframe is like, but also what the csv file is like. The dataframe looks all right, but the csv file is not. That's the confusing part.
[更新]
许多答案说我应该使用df.columns=['values']
. 嗯,这对我不起作用。我不仅关心数据帧是什么样的,还关心 csv 文件是什么样的。数据框看起来没问题,但 csv 文件不是。这就是令人困惑的部分。
...
df.columns=["values"]
df.to_csv("output.csv")
It says:IOError: [Errno 13] Permission denied: 'output.csv'
.
I then use the absolute path "C:\Users\myname\Desktop\output.csv", the error is like: IOError: [Errno 13] Permission denied: 'C:\\Users\\myname\\Desktop\\output.csv'
I don't know why this error, but this is rather confusing.
For further information, I installed anaconda-2.7 on win10. I tested the code with spyder.
它说:IOError: [Errno 13] Permission denied: 'output.csv'
。
然后我使用绝对路径“C:\Users\myname\Desktop\output.csv”,错误是这样的:IOError: [Errno 13] Permission denied: 'C:\\Users\\myname\\Desktop\\output.csv'
我不知道为什么会出现这个错误,但这很令人困惑。
有关更多信息,我在 win10 上安装了 anaconda-2.7。我用spyder测试了代码。
回答by jezrael
You can set column name in DataFrame
constructor:
您可以在DataFrame
构造函数中设置列名:
df = pd.DataFrame(myseries, columns=['values'])
df.to_csv("output.csv")
Or:
或者:
df = pd.DataFrame({'values':myseries})
print (df)
values
0 -0.429758
1 -0.019931
2 1.189596
3 1.309223
4 -0.337061
df.to_csv("output.csv")
Or set parameter header
in DataFrame.to_csv
:
也可以设置参数header
在DataFrame.to_csv
:
df = pd.DataFrame(myseries)
df.to_csv("output.csv", header=['values'])
Or in Series.to_csv
:
myseries.to_csv("output.csv", header=['values'])
回答by Stephanie Marker
To set the column name to 'values' try:
要将列名设置为“值”,请尝试:
df.columns = ['values']
回答by Sachin Mahesh
It gives the below error :
它给出了以下错误:
says:IOError: [Errno 13] Permission denied: 'output.csv'.
I was getting the same error, and the issue was that I had opened the file in Excel and hence when my program wanted to write to the same directory with the same filename, I was getting this error.
我遇到了同样的错误,问题是我在 Excel 中打开了文件,因此当我的程序想要使用相同的文件名写入同一个目录时,我收到了这个错误。