Pandas to_csv调用正在添加一个逗号 [英] Pandas to_csv call is prepending a comma

查看:1141
本文介绍了Pandas to_csv调用正在添加一个逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据文件apples.csv,具有头像:

I have a data file, apples.csv, that has headers like:

"id","str1","str2","str3","num1","num2"



< with pandas:

I read it into a dataframe with 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).

然后我将其保存:

apples.to_csv('bananas.csv',columns=["id","str1","str2","str3","num1","num2"])

现在,看看bananas.csv,其标题是:

Now, looking at bananas.csv, its headers are:

,id,str1,str2,str3,num1,num2


b $ b

没有更多的引号(我不关心,因为它不影响文件中的任何内容),然后引导逗号。
接下来的行现在有一个额外的列,所以它保存了7列。但如果我这样做:

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:

print(len(apples.columns))

在保存之前,它显示6列...

Immediately prior to saving, it shows 6 columns...

Perl / R,并且对Python和特别是熊猫经验不多,所以我不知道这是是的,它只是这样或什么问题是 - 但我花了有趣的很长时间试图找出,没有找到它通过搜索。

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.

如何让它不要做那个前面加逗号,也许同样重要 - 为什么会这样做?

How can I get it to not do that prepending of a comma, and maybe as important - why is it doing it?

推荐答案

设置 index = False (默认值为 True 因此为什么你看到这个输出),以便它不保存索引值到您的csv,请参阅 docs

Set index=False (the default is True hence why you see this output) so that it doesn't save the index values to your csv, see the docs

因此:

df = pd.DataFrame({'a':np.arange(5), 'b':np.arange(5)})
df.to_csv(r'c:\data\t.csv')

/ p>

results in

,a,b
0,0,0
1,1,1
2,2,2
3,3,3
4,4,4

此时:

df.to_csv(r'c:\data\t.csv', index=False)

会导致:

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

这篇关于Pandas to_csv调用正在添加一个逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆