'Pandas read_csv parsing '0' as FALSE in particular columns only
I parsed the csv through read_csv and then saved it to disk, snapshots are from the resulting file.
Snapshot from Excel.
Snapshot with FALSE filter.
Notice that there are 0s in other columns that aren't changed to FALSE. Only in this particular column.
target_metro = pd.read_csv('/home/output/data.csv', dtype='unicode')
I'm using this command to read the file.
I tried converting the format of column to 'Number' instead of 'General' which didn't help since csv have string values. I have pinpointed this error to parsing from read_csv.
I want to add that pandas shows a mixed dtype warning for two columns, those end up with FALSE values.
What is going wrong here?
Solution 1:[1]
You can replace like this:
df["column_Name"] = df["column_Name"].astype(int)
or
df['column_Name'].replace({False: 0}, inplace=True)
or
df = df['column_Name']*1
or
df['column_Name'] = df['column_Name'].map({ 'False': 0})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
