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:
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 ideaSo 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
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.
The to_csv() function is used to write object to a comma-separated values (csv) file.
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' .
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With