Pandas : to_csv() got an unexpected keyword argument
Would this help:
pd.to_csv('test.csv', quoting=csv.QUOTE_NONE)
As per your comment, read docs on series.
You can use to_frame
before saving to resolve your issue.

TheWho
Updated on July 09, 2022Comments
-
TheWho 6 months
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'
df.to_csv('transactions.x', header=False, doublequote=False)
ordf.to_csv('transactions.x', doublequote=False)
My pandas version is 0.19.2 (Checked with
print(pd.__version__)
) I am usingPython 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
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.In order to cast series to dataframe, this command
df = df.groupby(['Transactions'])['Items'].apply(','.join).to_frame()
should be used instead.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.Hopefully, it helps for everyone. Thanks
-
TheWho over 5 yearsSame issue, it still throws a same TypeError issue.
-
TheWho over 5 yearsNo, all the parameters (quoting, doublequote, etc...) are becoming invalidated since somehow dataframe behaves as a series.
-
TheWho over 5 yearsThank you but, By typing following command
df = df.groupby(['Transactions'])[['Items']].apply(','.join)
throws the same error, on the other hand if I want to usedf = df.groupby(['Transactions'])[['Items']].apply(','.join).to_frame()
, it does not throw error but output is showing the "Items" instead of real items such as a, b, c, .... If i use the last option,df = df.groupby(['Transactions'])['Items'].apply(','.join).to_frame()
, it thows the followingerror _csv.Error: need to escape, but no escapechar set
-
TheWho over 5 yearsI already tried them Thank you but, By typing following command df = df.groupby(['Transactions'])[['Items']].apply(','.join) throws the same error, on the other hand if I want to use df = df.groupby(['Transactions'])[['Items']].apply(','.join).to_frame() , it does not throw error but output is showing the "Items" instead of real items such as a, b, c, .... If i use the last option, df = df.groupby(['Transactions'])['Items'].apply(','.join).to_frame() , it thows the following error _csv.Error: need to escape, but no escapechar set
-
matanster over 4 yearsTo turn a series to a data frame of a single column, I think you should rather use
to_frame
as suggested in @zipa's answer below, or at least I'm not sure myself why you need that elaborate way that you use here.