You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current pandas behavior matches python stdlib csv. The reason quoting becomes necessary is to distinguish a 1-element row with the single empty field from an empty row.
@mvashishtha try this updated code, which ensures consistency, specify the na_rep parameter in to_csv method. This parameter determines how missing values are represented in the CSV file.
import pandas as pd
df = pd.DataFrame([['a', 0], [None, 1]], columns=['col0', 'col1'])
df.to_csv('all_columns.csv', index=False, columns=['col0', 'col1'], na_rep='NaN')
print(f'CSV from writing all columns:\n{open("all_columns.csv").read()}')
df.to_csv('first_column.csv', index=False, columns=['col0'], na_rep='NaN')
print(f'CSV from writing first column:\n{open("first_column.csv").read()}")
Hope this helps, plz let me know if it works
Thanks
Maybe pandas should warn users when they write a single column csv with None values? This behavior isn't intuitive and looks inconsistent with the case of multiple columns even though it follows the csv.writer logic.
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
The
None
in the dataframe becomes an empty string when writing both columns, but""
when writing just the first column.Expected Behavior
Should be consistent and use the representation for missing data in
na_rep
in both cases.Installed Versions
versions
The text was updated successfully, but these errors were encountered: