Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

pd.Series([my_list]).to_csv('output.tsv',sep='\t',index=False,header=False) 

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?

like image 742
user5359531 Avatar asked Jul 15 '16 23:07

user5359531


People also ask

How do you write TSV in pandas?

How To Write Pandas DataFrame as TSV File? We can use Pandas' to_csv() function to write dataframe as Tab Separated Value or TSV file by specifying the argument for separator with sep=”\t”.

How do you write a series file in Python?

to_csv() function write the given series object to a comma-separated values (csv) file/format. Parameter : path_or_buf : File path or object, if None is provided the result is returned as a string.

How do I display a TSV file in Python?

The very simple way to read data from TSV File in Python is using split(). We can read a given TSV file and store its data into a list.

How do I convert a pandas series to a list?

How to use the tolist() method to convert pandas series to list. To convert a pandas Series to a list, simply call the tolist() method on the series which you wish to convert.


1 Answers

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,                        decimal=decimal)     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) 

or:

your_series_obj.to_frame().to_csv(..., quoting=csv.QUOTE_NONE) 
like image 114
MaxU - stop WAR against UA Avatar answered Sep 22 '22 01:09

MaxU - stop WAR against UA