我想将pandas数据框附加到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
答案 0 :(得分:7)
您必须在保存到csv之前连接数据帧,因为您必须知道所有生成的列才能正确保存数据,而每个数据帧本身都不知道。以下将做:
>>> 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
答案 1 :(得分: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)