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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:30:34  来源:igfitidea点击:

Pandas : to_csv() got an unexpected keyword argument

pythonpandasexport-to-csv

提问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

Would this help:

这是否有帮助:

pd.to_csv('test.csv', quoting=csv.QUOTE_NONE)

As per your comment, read docson series.

根据您的评论,阅读系列文档

You can use to_framebefore saving to resolve your issue.

您可以to_frame在保存之前使用来解决您的问题。

回答by EdChum

Your groupbycall generates a series for which there is no doublequoteparam, 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 applythis will return a Serieshence 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

希望对大家有所帮助。谢谢