Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pandas Read CSV set dtype

When recognizing integers read_csv will default to int64, but if we would like to save memory we can set the dtype while we read the file.

import pandas as pd
import numpy as np

df = pd.read_csv('mixed.csv', dtype = { 'MyInteger' : np.int8 })
print( df.dtypes )

MyText        object
MyInteger       int8
MyFloat      float64
MyBool          bool
MyExit         int64
dtype: object