Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV dialect in pandas DataFrame to_csv (python)

I'm happy to use csv.Dialect objects for reading and writing CSV files in python. My only problem with this now is the following:

  • it seems like I can't use them as a to_csv parameter in pandas
  • to_csv and Dialect (and read_csv) parameters are different (eg. to_csv have sep instead of delimiter)... so generating a key-value parameterlist doesn't seem to be a good idea

So I'm a little lost here, what to do.

What can I do if I have a dialect specified but I have a pandas.DataFrame I have to write into CSV? Should I create a parameter mapping by hand?! Should I change to something else from to_csv?

I have pandas-0.13.0.


Note: to_csv(csv.reader(..., dialect=...), ...) didn't work:

need string or buffer, _csv.writer found

like image 416
masu Avatar asked Jan 28 '14 16:01

masu


People also ask

What does To_csv do in pandas?

Pandas DataFrame to_csv() function converts DataFrame into CSV data. We can pass a file object to write the CSV data into a file. Otherwise, the CSV data is returned in the string format.

What does To_csv mean in Python?

The to_csv() function is used to write object to a comma-separated values (csv) file.

Does pandas To_csv overwrite?

When you write pandas DataFrame to an existing CSV file, it overwrites the file with the new contents. To append a DataFrame to an existing CSV file, you need to specify the append write mode using mode='a' .

How do you write into CSV file in Python using pandas?

You can also pass custom header names while reading CSV files via the names attribute of the read_csv() method. Finally, to write a CSV file using Pandas, you first have to create a Pandas DataFrame object and then call to_csv method on the DataFrame.


1 Answers

If you have a CSV reader, than you don't need to also do a pandas.read_csv call. You can create a dataframe with a dictionary, so your code would look something like:

csv_dict = # Insert dialect code here to read in the CSV as a dictonary of the format {'Header_one': [1, 2, 3], 'Header_two': [4, 5, 6]}
df = pd.DataFrame(csv_dict)
like image 134
Nate Rush Avatar answered Oct 01 '22 03:10

Nate Rush