问题描述
我无法将pandas数据帧的条目写入stringbuffer。
I'm having trouble writing the entries of a pandas dataframe to a stringbuffer.
可以通过将stringbuffer传递给read_csv函数来初始化数据帧。
It's possible to initialize a dataframe by passing a stringbuffer to the read_csv function.
In [80]: buf = StringIO('a,b\n1,2\n')
In [81]: df = pandas.read_csv(buf)
In [82]: df
Out[82]:
a b
0 1 2
要做到相反的事情不是直截了当的,因为 DataFrame.to_csv
函数只接受字符串文件路径。
To do the opposite is not straight forward as the DataFrame.to_csv
function only accepts a string file path.
这种行为有什么好的理由吗?序列化pandas DataFrame而不先将内容存储在磁盘上的最佳方法是什么?
Is there any good reason for this behaviour? What's the best way to serialize a pandas DataFrame without storing the contents on disk first?
推荐答案
这几乎只是一个疏忽/不一致性。我在这里为它创建了一个GitHub问题:
This is pretty much just an oversight / inconsistency. I created a GitHub issue here for it:
编辑:今天实现了这一点,因此您可以将StringIO传递给 to_csv
,现在在git master中,将成为即将发布的0.7.0版本的一部分
Implemented this today so you can pass a StringIO to to_csv
, in git master now and will be part of forthcoming 0.7.0 release
这篇关于Pandas DataFrame序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!