简体   繁体   English

覆盖目录中的.csv文件

[英]Overwrite .csv file in directory

I was wondering how I can overwrite an existing.csv file in the same file directory called test.csv我想知道如何在名为 test.csv 的同一文件目录中覆盖现有的.csv 文件

My following program opens the test.csv file it then appends some string names to it and is then called "main" dataframe.我的以下程序打开 test.csv 文件,然后将一些字符串名称附加到它,然后称为“主”dataframe。 I was wondering how I can save "main" to the same directory as test.csv.我想知道如何将“main”保存到与 test.csv 相同的目录中。 Therefore replacing the old test.csv with the new one.因此用新的替换旧的test.csv。

import pandas as pd

main=pd.read_csv('C:/Users/Jonas/Desktop/testfile/test.csv', header=None)

target_col = ['dog'] * main.shape[0]
target_col[0] = 'target'
main.insert(loc = 0, column = -1, value = target_col)
main.rename(columns = lambda x:x+1,inplace=True) #Reorients -1 to the origin
#How to rewrite test.csv?

Image showing the old test.csv file and the new "Final" dataframe: enter image description here显示旧测试的图像。csv 文件和新的“最终”dataframe:在此处输入图像描述

Thanks.谢谢。 Below shows the saved data error: enter image description here下面显示了保存的数据错误:在此处输入图像描述

Use to_csv使用to_csv

main.to_csv('C:/Users/Jonas/Desktop/testfile/test.csv', header=None)
 main.to_csv('C:/Users/Jonas/Desktop/testfile/test.csv', header=None, index=False)

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

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