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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38406328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:45:38  来源:igfitidea点击:

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

pythonpandasdataframe

提问by user5359531

I have read the manual hereand saw thisanswer, 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?

知道发生了什么吗?

回答by MaxU

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

pandasSeries.to_csv()内部实现首先将 Series 转换为 DataFrame,然后调用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

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)