Pandas Read CSV convert values


Sometime the data in the CSV file represents something else or we might want to change the meaning of the data.

For example in some cases 0 represents False and 1 represents True. If the CSV file contains 0 and 1 values in a column Pandas will automatically represent them as integers. We can convert them to False and True values respectively.

In another case we might have exit-codes in a column where 0 means success and any other number means failure. We might want to simplify that column and represent success by True and failure by False. (Yes, we loose the details of the failure, but maybe we are not interested in the details.)

This latter is what we can see in our example.


examples/pandas/mixed_convert_values.py
import pandas as pd
import numpy as np

df = pd.read_csv('mixed.csv', converters = { 'MyExit' : lambda x : x == '0' })
print( df.dtypes )
print( df )

MyText        object
MyInteger      int64
MyFloat      float64
MyBool          bool
MyExit          bool
dtype: object
  MyText  MyInteger  MyFloat  MyBool  MyExit
0    Joe         12      3.4    True    True
1   Jane          3      4.0   False   False
2   Mary          7      2.3   False   False