df.to_csv('transactions.x', header=False, doublequote=False)
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'
My pandas version is 0.19.2 (Checked with print(pd.version)) I am using 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
Do you guys have any idea about it?
Thank you.
Output of pd.show_versions()
Comment From: TomAugspurger
Would df
happen to be a Series instead of a DataFrame
? Their writers aren't 100% equivalent.
Comment From: jackalhan
Actually,
After I execute the following command, df = pd.read_sql (......, connection)
I can call df.to_csv('transactions.x', header=False, doublequote=False)
successfully because the result is a dataframe object,
but when I continue to do something with df object such as
df = df.groupby(['Transactions'])['Items'].apply(','.join)
, the command df.to_csv('transactions.x', header=False, doublequote=False)
becomes invalidated. So, how to cast the apply function to the dataframe in order to use these optional parameters?
Thank you.
Comment From: TomAugspurger
Either make your slice ['Items']
return a dataframe with [['Items']]
or convert the series to a DataFrame with ['Item'].to_frame()
Comment From: jackalhan
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
Comment From: jackalhan
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