简体   繁体   English

Pandas Dataframe to_csv不会在Mac和Linux上附加(在Windows中会附加)

[英]Pandas Dataframe to_csv does not append on Mac and Linux (does it in Windows)

I need to write a csv file with Pandas Dataframe appending csv rows. 我需要使用Pandas Dataframe附加csv行编写一个csv文件。

I have written this piece of code that works fine in Windows. 我已经编写了这段在Windows上可以正常运行的代码。 However, when I run it on our Linux Server or on Mac OSX it overwrites the file instead appending the next line. 但是,当我在Linux服务器或Mac OSX上运行它时,它将覆盖文件,而不是附加下一行。

 with open("Metrics-2SQUIDS.csv", "a+") as f:
    metrics_tosave.to_csv(f, columns=['C1', 'C2', 'C3'], sep=',', index=False)

I also tried with "a" instead of "a+". 我也尝试用“ a”代替“ a +”。 Both work fine with Windows but not in Mac or Linux. 两者都可以在Windows上正常运行,但不能在Mac或Linux上运行。 Is there any other test I can do? 我还能做其他测试吗?

You need mode optional argument to to_csv : 您需要to_csv mode可选参数:

metrics_tosave.to_csv(f, mode='a', columns=['C1', 'C2', 'C3'], sep=',', index=False)

And I don't think you need with open() as f with this. 而且我认为您不需要with open() as f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM