Reputation: 867
I have a .csv file which I can read using pd.read_csv()
.
In this file i have SomeBoolean
column that I need to update with new logic.
However when I write the file again using pd.to_csv()
some other column values are changed, e.g. the sometimesNaN
column has some empty values which are interpreted as NaN
while reading the csv, but then they are also written as NaN
instead of an empty value.
Is there a way in which I can update the SomeBoolean
column without accidentally affecting the others such as the sometimesNaN
column?
Index Date SomeBoolean Values sometimesNaN
0 2021-05-18 False 216.351155 NaN
1 2021-05-18 False 876.222176 NaN
2 2021-05-18 False 767.214479 NaN
Upvotes: 2
Views: 353
Reputation: 23217
You can consider to make Pandas not to interpret empty values in sometimesNaN
column as NaN
so that it won't overwritten original empty values when written back.
To achieve this, you can specify the parameter keep_default_na=False
in reading in the csv during pd.read_csv()
call.
keep_default_na : bool, default True
Whether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows:
If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing.
If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing.
If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing.
If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN.
Note that this behavior applies to all columns. So if you have NaN
values in other columns you want Pandas to interpret the empty values as NaN
when reading in the csv, you may not be able to use this.
Upvotes: 2