[英]How can i save a pandas dataframe to csv in overwrite mode?
I want to save pandas dataframe to csv in overwrite mode.我想在覆盖模式下将 pandas dataframe 保存到 csv 。 I want that whenever the program will run again with any changes then it should save the dataframe to csv and overwrite the already saved csv file at the location.
我希望每当程序通过任何更改再次运行时,它都应该将 dataframe 保存到 csv 并覆盖该位置已经保存的 csv 文件。
I think you can do:我认为你可以这样做:
df.to_csv('file_name.csv',mode='w+' )
Form more info goto documentation形成更多信息转到文档
This should overwrite the file by default:默认情况下,这应该会覆盖文件:
df.to_csv("filename")
There are many ways you can do that.有很多方法可以做到这一点。
This replaces file everytime you run the script.每次运行脚本时都会替换文件。
dataframe.to_csv(r"C:\....\notebooks\file.csv")
This method first opens the files,gives you options of reading(r), appending(ab) or writing.此方法首先打开文件,为您提供读取(r)、附加(ab)或写入的选项。
import csv进口 csv
with open ('file.csv','w') as f:
wtr = csv.writer(f)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.