Skip to main content

Data Types and Missing Values

Dtypes​

The data type for a column in a DataFrame or a Series is known as the dtype.

You can use the dtype property to grab the type of a specific column. For instance, we can get the dtype of the price column in the reviews DataFrame

reviews.price.dtype
--
dtype('float64')

Alternatively, the dtypes property returns the dtype of every column in the DataFrame:

reviews.dtypes

astype()​

It's possible to convert a column of one type into another wherever such a conversion makes sense by using the astype() function. For example, we may transform the points column from its existing int64 data type into a float64 data type:

reviews.points.astype('float64')

Missing data​

Entries missing values are given the value NaN, short for "Not a Number". For technical reasons these NaN values are always of the float64 dtype

notnull() or isnull()​

Pandas provides some methods specific to missing data. To select NaN entries you can use pd.isnull() (or its companion pd.notnull()). This is meant to be used thusly:

reviews[pd.isnull(reviews.country)]

fillna()​

Replacing missing values is a common operation. Pandas provides a really handy method for this problem: fillna(). fillna() provides a few different strategies for mitigating such data. For example, we can simply replace each NaN with an "Unknown"

reviews.region_2.fillna("Unknown")

dropna()​

  • Remove missing values
  • Parameters:

axis{0 or ‘index’, 1 or ‘columns’}, default 0

reviews.dropna(axix=0)

replace()​

Alternatively, we may have a non-null value that we would like to replace. For example, suppose that since this dataset was published, reviewer Kerin O'Keefe has changed her Twitter handle from @kerinokeefe to @kerino. One way to reflect this in the dataset is using the replace() method:

reviews.taster_twitter_handle.replace("@kerinokeefe", "@kerino")