Pandas:to_csv() 得到了一个意外的关键字参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43684096/
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
Pandas : to_csv() got an unexpected keyword argument
提问by TheWho
While I am trying to use some of the parameters in dataframe to_csv function, it throws an TypeError, such as `TypeError: to_csv() got an unexpected keyword argument 'doublequote'
当我尝试使用数据帧 to_csv 函数中的一些参数时,它抛出了一个类型错误,例如 `TypeError: to_csv() got an unexpected keyword argument 'doublequote'
df.to_csv('transactions.x', header=False, doublequote=False)
or
df.to_csv('transactions.x', doublequote=False)
df.to_csv('transactions.x', header=False, doublequote=False)
或者
df.to_csv('transactions.x', doublequote=False)
My pandas version is 0.19.2 (Checked with print(pd.__version__)
)
I am using Python 3.5
我的Pandas版本是 0.19.2 (Checked with print(pd.__version__)
) 我正在使用Python 3.5
The following official document is based on 0.19.2. Although, I am having type errors, it is stated that these parameters can be used as an optional. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
以下官方文档基于0.19.2。尽管我遇到了类型错误,但据说这些参数可以用作可选参数。 http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html
Do you guys have any idea about it?
你们有什么想法吗?
Thank you.
谢谢你。
SOLUTION
解决方案
Thanks for brain storming with all commenters.
感谢您与所有评论者进行头脑风暴。
After using following the command df = df.groupby(['Transactions'])['Items'].apply(','.join)
, dataframe becomes series.
使用以下命令后df = df.groupby(['Transactions'])['Items'].apply(','.join)
,数据框变为系列。
In order to cast series to dataframe, this command df = df.groupby(['Transactions'])['Items'].apply(','.join).to_frame()
should be used instead.
为了将系列转换为数据帧,df = df.groupby(['Transactions'])['Items'].apply(','.join).to_frame()
应改用此命令。
Finally, to export it as a CSV with non-quote style by avoiding escape char, you need to end up with the following command
df.to_csv('transactions.x', header=False, quoting=csv.QUOTE_NONE, escapechar=' ')
#or whatever escapechar.
最后,要通过避免转义字符将其导出为非引用样式的 CSV,您需要以以下命令
df.to_csv('transactions.x', header=False, quoting=csv.QUOTE_NONE, escapechar=' ')
结束 # 或任何转义字符。
Hopefully, it helps for everyone. Thanks
希望对大家有所帮助。谢谢
回答by zipa
回答by EdChum
Your groupby
call generates a series for which there is no doublequote
param, convert to a DataFrame calling to_frame()
prior to calling to_csv
您的groupby
调用会生成一个没有doublequote
参数的系列,在调用to_frame()
之前转换为 DataFrame调用to_csv
This:
这个:
df.groupby(['Transactions'])['Items'].apply(','.join)
is grouping your df, but then you select a single column and call apply
this will return a Series
hence your error
正在对您的 df 进行分组,但随后您选择一列并调用apply
这将返回一个Series
因此您的错误
回答by TheWho
Thanks for brain storming with all commenters.
感谢您与所有评论者进行头脑风暴。
After using following the command df = df.groupby(['Transactions'])['Items'].apply(','.join)
, dataframe becomes series.
使用以下命令后df = df.groupby(['Transactions'])['Items'].apply(','.join)
,数据框变为系列。
In order to cast series to dataframe, this command df = df.groupby(['Transactions'])['Items'].apply(','.join).to_frame()
should be used instead.
为了将系列转换为数据帧,df = df.groupby(['Transactions'])['Items'].apply(','.join).to_frame()
应改用此命令。
Finally, to export it as a CSV with non-quote style by avoiding escape char, you need to end up with the following command df.to_csv('transactions.x', header=False, quoting=csv.QUOTE_NONE, escapechar=' ')
#or whatever escapechar.
最后,要通过避免转义字符将其导出为非引用样式的 CSV,您需要以以下命令 df.to_csv('transactions.x', header=False, quoting=csv.QUOTE_NONE, escapechar=' ')
结束 # 或任何转义字符。
Hopefully, it helps for everyone. Thanks
希望对大家有所帮助。谢谢