[英]How to write/read pandas Series to/from csv?
I can't figure out how to write/read a Series correctly...The following (and many variations of it) results in the read series being different than the written series...note that the series is read into a DataFrame rather than a series. 我无法弄清楚如何正确地编写/读取系列...以下(以及它的许多变体)导致读取系列与书面系列不同...请注意,该系列被读入DataFrame而不是比一系列。
In [55]: s = pd.Series({'a': 1, 'b': 2})
In [56]: s
Out[56]:
a 1
b 2
In [57]: s.to_csv('/tmp/s.csv')
In [58]: !cat /tmp/s.csv
a,1
b,2
In [59]: pd.read_csv('/tmp/s.csv')
Out[59]:
a 1
0 b 2
In [3]: s.to_csv('/home/wesm/tmp/sfoo.csv')
In [4]: Series.from_csv('/home/wesm/tmp/sfoo.csv')
Out[4]:
a 1
b 2
You can also pass header=None, index_col=0, squeeze=True
to read_csv
similar to what Rutger Kassies suggested. 你也可以将
header=None, index_col=0, squeeze=True
传递给read_csv
类似于Rutger Kassies所建议的。
A CSV doesnt contain any information about the structure of your pandas Series. CSV不包含有关您的pandas系列结构的任何信息。 Specifying some extra arguments might help.
指定一些额外的参数可能有所帮助。 Getting the data back as normal is possible with:
可以通过以下方式恢复数据:
pd.read_csv('s.csv', index_col=0, header=None)
But that adds default column and index names to it. 但是它会为其添加默认的列和索引名称。 If you just want to save your Series/DF for later use its better to use the .save() and pd.load() methods.
如果您只想保存Series / DF以供以后使用,最好使用.save()和pd.load()方法。
Saving a pandas object to a file, and then re-creating that object from a file, you would use: 将pandas对象保存到文件,然后从文件重新创建该对象,您将使用:
s.to_pickle('filename')
and 和
s = pd.read_pickle('filename')
methods. 方法。
Here's the details: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_pickle.html 以下是详细信息: https : //pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_pickle.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.