Changing the datatype while reading data

We just changed the datatype of the column after the data has been read into pandas. Alternatively, we can change the datatype while reading in the data. For this, we pass the column name and the datatype into the column we want to change to the read data method. The column we wanted in float has been imported as float64:

data2 = pd.read_csv('data-zillow.csv', sep=',', dtype={'Zhvi':float}) 

The following is the output:

Get Mastering Exploratory Analysis with pandas now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.