Python 如何将熊猫数据添加到现有的 csv 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17530542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add pandas data to an existing csv file?
提问by Ayoub Ennassiri
I want to know if it is possible to use the pandas to_csv()
function to add a dataframe to an existing csv file. The csv file has the same structure as the loaded data.
我想知道是否可以使用 pandasto_csv()
函数将数据框添加到现有的 csv 文件中。csv 文件与加载的数据具有相同的结构。
采纳答案by tlingf
回答by Andy Hayden
You can appendto a csv by opening the filein append mode:
您可以通过以追加模式打开文件来追加到 csv :
with open('my_csv.csv', 'a') as f:
df.to_csv(f, header=False)
If this was your csv, foo.csv
:
如果这是您的 csv,则foo.csv
:
,A,B,C
0,1,2,3
1,4,5,6
If you read that and then append, for example, df + 6
:
例如,如果您阅读该内容然后附加df + 6
:
In [1]: df = pd.read_csv('foo.csv', index_col=0)
In [2]: df
Out[2]:
A B C
0 1 2 3
1 4 5 6
In [3]: df + 6
Out[3]:
A B C
0 7 8 9
1 10 11 12
In [4]: with open('foo.csv', 'a') as f:
(df + 6).to_csv(f, header=False)
foo.csv
becomes:
foo.csv
变成:
,A,B,C
0,1,2,3
1,4,5,6
0,7,8,9
1,10,11,12
回答by KCzar
A little helper function I use with some header checking safeguards to handle it all:
我使用一个小辅助函数和一些标题检查保护措施来处理这一切:
def appendDFToCSV_void(df, csvFilePath, sep=","):
import os
if not os.path.isfile(csvFilePath):
df.to_csv(csvFilePath, mode='a', index=False, sep=sep)
elif len(df.columns) != len(pd.read_csv(csvFilePath, nrows=1, sep=sep).columns):
raise Exception("Columns do not match!! Dataframe has " + str(len(df.columns)) + " columns. CSV file has " + str(len(pd.read_csv(csvFilePath, nrows=1, sep=sep).columns)) + " columns.")
elif not (df.columns == pd.read_csv(csvFilePath, nrows=1, sep=sep).columns).all():
raise Exception("Columns and column order of dataframe and csv file do not match!!")
else:
df.to_csv(csvFilePath, mode='a', index=False, sep=sep, header=False)
回答by ai-shwarya
A bit late to the party but you can also use a context manager, if you're opening and closing your file multiple times, or logging data, statistics, etc.
参加聚会有点晚了,但如果您多次打开和关闭文件,或者记录数据、统计信息等,您也可以使用上下文管理器。
from contextlib import contextmanager
import pandas as pd
@contextmanager
def open_file(path, mode):
file_to=open(path,mode)
yield file_to
file_to.close()
##later
saved_df=pd.DataFrame(data)
with open_file('yourcsv.csv','r') as infile:
saved_df.to_csv('yourcsv.csv',mode='a',header=False)`
回答by Grant Shannon
Initially starting with a pyspark dataframes - I got type conversion errors (when converting to pandas df's and then appending to csv) given the schema/column types in my pyspark dataframes
最初从 pyspark 数据帧开始 - 考虑到我的 pyspark 数据帧中的架构/列类型,我遇到了类型转换错误(当转换为 Pandas df 然后附加到 csv 时)
Solved the problem by forcing all columns in each df to be of type string and then appending this to csv as follows:
通过强制每个 df 中的所有列都是字符串类型,然后将其附加到 csv 解决了这个问题,如下所示:
with open('testAppend.csv', 'a') as f:
df2.toPandas().astype(str).to_csv(f, header=False)
回答by DeveScie
with open(filename, 'a') as f:
df.to_csv(f, header=f.tell()==0)
- Create file unless exists, otherwise append
- Add header if file is being created, otherwise skip it
- 创建文件除非存在,否则追加
- 如果正在创建文件,则添加标题,否则跳过它