>>> 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'
没有引用参数,它可以工作。
pd.Series([my_list]).to_csv('output.tsv',sep='\t',index=False,header=False)
但这与我的预期用法不符。
为了让事情变得更加混乱,当我以这种方式写出一张桌子时,没有引号,也没有错误:
my_dataframe.to_csv('output2.tsv',sep='\t', quoting=csv.QUOTE_NONE)
知道发生了什么事吗?
答案 0 :(得分:8)
internal pandas implementation of Series.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
所以你可以自己转换,然后你会有更丰富的参数:
pd.DataFrame(your_series_obj).to_csv(..., quoting=csv.QUOTE_NONE)
或:
your_series_obj.to_frame().to_csv(..., quoting=csv.QUOTE_NONE)