TheWho
TheWho

Reputation: 839

Pandas : to_csv() got an unexpected keyword argument

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) or 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

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

Upvotes: 3

Views: 42055

Answers (4)

Michael B. Currie
Michael B. Currie

Reputation: 14656

I got this "TypeError: NDFrame.to_csv() got an unexpected keyword argument 'line_terminator'" on this command:

df0.to_csv(file_path, line_terminator='\n')

The reason is pandas in its infinite wisdom changed the identifier name:

lineterminator: "Changed in version 1.5.0: Previously was line_terminator, changed for consistency with read_csv and the standard library ‘csv’ module."

Upvotes: 1

TheWho
TheWho

Reputation: 839

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

Upvotes: 0

zipa
zipa

Reputation: 27869

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.

Upvotes: 3

EdChum
EdChum

Reputation: 393963

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

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

Upvotes: 0

Related Questions