如何从Pandas系列中正确写出TSV文件?

use*_*531 11 python dataframe pandas

我在这里阅读了手册并看到了这个答案,但它没有用:

>>> 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'
Run Code Online (Sandbox Code Playgroud)

如果没有引用参数,它就可以工作.

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

但这与我的预期用法不符.

为了让事情更加令人困惑,当我以这种方式写出一张表时,没有引号,也没有错误:

my_dataframe.to_csv('output2.tsv',sep='\t', quoting=csv.QUOTE_NONE)
Run Code Online (Sandbox Code Playgroud)

知道发生了什么事吗?

Max*_*axU 11

内部大熊猫实施Series.to_csv()第一系列转换到数据帧,然后调用DataFrame.to_csv()方法:

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
Run Code Online (Sandbox Code Playgroud)

所以你可以自己转换它然后你会有一组更丰富的参数:

pd.DataFrame(your_series_obj).to_csv(..., quoting=csv.QUOTE_NONE)
Run Code Online (Sandbox Code Playgroud)

要么:

your_series_obj.to_frame().to_csv(..., quoting=csv.QUOTE_NONE)
Run Code Online (Sandbox Code Playgroud)