2014-01-07 282 views
1

我想将熊猫数据框附加到CSV文件的末尾。棘手的部分是当我追加行时,某些列的时间可能不同。我想这样的熊猫数据框为CSV

a = pd.DataFrame([[1, 2]], columns= ["one", "two"]) 
with open("learn.csv", "w") as f: 
    a.to_csv(f, header=True) 

a = pd.DataFrame([[1, 2]], columns= ["one", "three"]) 
with open("learn.csv", "a") as f: 
    a.to_csv(f) 

代码生成CSV文件看起来像这样:

one, two, three 
1, 2, None 
1, None, 2 
+4

最好的办法是将DataFrames连接到一个表示所有列的数据框中。否则,您不只是将行“添加到CSV文件的末尾”,您必须返回并更改标题。 – BrenBarn

回答

6

你必须来连接到保存到CSV之前你dataframes,因为你必须知道的所有结果列能够正确地保存数据,这对每个数据帧都是未知的。以下将做:

>>> from StringIO import StringIO 
>>> buf = StringIO() 
>>> a = pd.DataFrame([[1, 2]], columns= ["one", "two"]) 
>>> b = pd.DataFrame([[1, 2]], columns= ["one", "three"]) 
>>> pd.concat([a, b]).to_csv(buf, index=None, na_rep='None') 
>>> print buf.getvalue() 
one,three,two 
1,None,2.0 
1,2.0,None 
0

这是我想出了使用alko的帖子和上面的评论的答案。 “a”是数据框:

if not os.path.isfile("learn.csv"): 
    with open("learn.csv", "w") as f: 
     a.to_csv(f, header=True, index=False) 
else: 
    reader = csv.reader(open("learn.csv")) 
    csv_col = set(reader.next()) 
    games_col = set(list(a.columns)) 
    if csv_col.issuperset(games_col): 
     with open("learn.csv", "a") as f: 
      a.to_csv(f, header=False, index=False) 
    else: 
     old_entries = pd.read_csv('learn.csv') 
     all_entries = pd.concat([old_entries, a]) 
     with open("learn.csv", "w") as f: 
      all_entries.to_csv(f, header=True, index=False)