Pandas DataFrame 转 CSV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20982165/
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
Pandas DataFrame to CSV
提问by appleLover
I want to append pandas data frames to the ends of CSV files. The tricky part is when I append rows, some of the times the columns may be different. I want code like this
我想将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)
to produce a CSV file that looks like this:
生成一个如下所示的 CSV 文件:
one, two, three
1, 2, None
1, None, 2
回答by alko
You have to concatenate your dataframes prior to saving to csv, as you have to know all the resulting columns to be able to save data properly, which is unknown to each dataframe alone. Following will do:
您必须在保存到 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
回答by appleLover
Here is the answer I came up with using alko's post and the comment from above. "a" is the dataframe:
这是我使用 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)

