user5359531 user5359531 - 1 year ago 134
Python Question

How to correctly write out a TSV file from a series in Pandas?

I have read the manual here and saw this answer, but it is not working:

>>> import pandas as pd
>>> import csv
>>> pd.Series([my_list]).to_csv('output.tsv',sep='\t',index=False,header=False, quoting=csv.QUOTE_NONE)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: to_csv() got an unexpected keyword argument 'quoting'

Without the quoting argument, it works.


But this is incompatible with my intended usage.

To make things even more confusing, when I wrote out a table this way, there were no quotes, and no errors:

my_dataframe.to_csv('output2.tsv',sep='\t', quoting=csv.QUOTE_NONE)

Any idea what is going on?

Answer Source

The internal pandas implementation of Series.to_csv() first converts Series to DataFrame and then calls DataFrame.to_csv() method:

def to_csv(self, path, index=True, sep=",", na_rep='', float_format=None,
           header=False, index_label=None, mode='w', nanRep=None,
           encoding=None, date_format=None, decimal='.'):
    Write Series to a comma-separated values (csv) file
    from pandas.core.frame import DataFrame
    df = DataFrame(self)
    # result is only a string if no path provided, otherwise None
    result = df.to_csv(path, index=index, sep=sep, na_rep=na_rep,
                       float_format=float_format, header=header,
                       index_label=index_label, mode=mode, nanRep=nanRep,
                       encoding=encoding, date_format=date_format,
    if path is None:
        return result

So you can convert it yourself and then you will have a richer set of parameters:

pd.DataFrame(your_series_obj).to_csv(..., quoting=csv.QUOTE_NONE)


your_series_obj.to_frame().to_csv(..., quoting=csv.QUOTE_NONE)