How to write/read pandas Series to/from csv?

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.


Saving a pandas object to a file, and then re-creating that object from a file, you would use:

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


A CSV doesnt contain any information about the structure of your pandas Series. 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.

Tags:

Python

Pandas