简体   繁体   English

如何在覆盖模式下将 pandas dataframe 保存到 csv?

[英]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.有很多方法可以做到这一点。

  1. This replaces file everytime you run the script.每次运行脚本时都会替换文件。

     dataframe.to_csv(r"C:\....\notebooks\file.csv")
  2. 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.

相关问题 如何将熊猫数据框保存到压缩的csv文件中? - how can I save a Pandas dataframe into a compressed csv file? 如何在 Pandas DataFrame 中为我的预测结果添加一列然后另存为 CSV? - How can I add a column for my predicted results in a Pandas DataFrame then save as a CSV? 如何将 pandas dataframe output 保存到新创建的文件夹中的 ZCC8D68C551C4A9A6D5313EDZDE 文件中? - How can I save pandas dataframe output to a CSV file in a newly created folder? 如何将熊猫数据框保存到具有特定文件名的.csv文件? - How to save pandas dataframe to a .csv with specific filename? 如何获取pandas数据帧对象值的模式? - How can I get mode(s) of pandas dataframe object values? 将 pandas dataframe 保存为 csv 并覆盖现有文件 - Saving pandas dataframe as csv and overwrite existing file 如何通过某些标准过滤 dataframe 然后保存。csv? - how can i filter a dataframe by some criteria and then save .csv? 如何将整个数据框保存在csv文件中? - How can I save an entire dataframe in a csv file? 如何将 dataframe 转换为 csv 并将其保存以返回? - How can I convert a dataframe to csv and save it to return it? 如何使用 Flask 为 Pandas 数据框设置“下载为 csv”按钮? - How can I set a "download as csv" button for a pandas dataframe with Flask?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM