Pandas to_csv 调用在前面加上一个逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30605909/
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 to_csv call is prepending a comma
提问by omgponies
I have a data file, apples.csv, that has headers like:
我有一个数据文件 apples.csv,其标题如下:
"id","str1","str2","str3","num1","num2"
I read it into a dataframe with pandas:
我用Pandas将它读入数据帧:
apples = pd.read_csv('apples.csv',delimiter=",",sep=r"\s+")
Then I do some stuff to it, but ignore that (I have it all commented out, and my overall issues still occurs, so said stuff is irrelevant here).
然后我对它做了一些事情,但忽略了它(我把它都注释掉了,我的整体问题仍然存在,所以说的东西在这里无关紧要)。
I then save it out:
然后我保存它:
apples.to_csv('bananas.csv',columns=["id","str1","str2","str3","num1","num2"])
Now, looking at bananas.csv, its headers are:
现在,看看bananas.csv,它的标题是:
,id,str1,str2,str3,num1,num2
No more quotes (which I don't really care about, as it doesn't impact anything in the file), and then that leading comma. The ensuing rows are now with an additional column in there, so it saves out 7 columns. But if I do:
没有更多的引号(我并不真正关心,因为它不会影响文件中的任何内容),然后是前导逗号。接下来的行现在有一个额外的列,所以它节省了 7 列。但如果我这样做:
print(len(apples.columns))
Immediately prior to saving, it shows 6 columns...
在保存之前,它显示了 6 列...
I am normally in Java/Perl/R, and less experienced with Python and particularly Pandas, so I am not sure if this is "yeah, it just does that" or what the issue is - but I have spent amusingly long trying to figure this out and cannot find it via searching.
我通常使用 Java/Perl/R,对 Python 尤其是 Pandas 的经验较少,所以我不确定这是“是的,它只是这样做”或问题是什么 - 但我花了很长时间试图弄清楚这个出来了,无法通过搜索找到它。
How can I get it to not do that prepending of a comma, and maybe as important - why is it doing it?
我怎样才能让它不加逗号,也许同样重要 - 为什么要这样做?
回答by EdChum
Set index=False(the default is Truehence why you see this output) so that it doesn't save the index values to your csv, see the docs
设置index=False(因此默认设置是True您看到此输出的原因),以便它不会将索引值保存到您的 csv,请参阅文档
So this:
所以这:
df = pd.DataFrame({'a':np.arange(5), 'b':np.arange(5)})
df.to_csv(r'c:\data\t.csv')
results in
结果是
,a,b
0,0,0
1,1,1
2,2,2
3,3,3
4,4,4
Whilst this:
虽然这个:
df.to_csv(r'c:\data\t.csv', index=False)
results in this:
结果如下:
a,b
0,0
1,1
2,2
3,3
4,4
It's for the situation where you may have some index values you want to save
适用于您可能有一些要保存的索引值的情况

