Conversion Function in Pandas:

Pandas is a package in Python which makes importing and analyzing easier in data science.

Casting Pandas object on specified dtype:

DataFrame.astype() is a function there for casting a pandas object to a specified dtype. The function astype() is also able to convert a suitable existing column to categorical type.

Code 1: Conversion of weight column data type.

Conversion Function in Pandas Python for Data Science - PST Analytics

https://www.pstanalytics.com/r-python-training

As we can see the above data has some NaN values so, in order to avoid any error, we will drop those NaN values.

Conversion Function in Pandas Python for Data Science - PST Analytics https://www.pstanalytics.com/r-python-training

Inferring better data type for input object column:

DataFrame.infer_objects() is a function which attempts to infer better data type for input object column. It attempts soft conversion of object dtyped columns and leaves non-convertible and non-object columns unchanged. The rules of inference are same as that of during normal series or data frame construction.

Code 1: Usinf infer_objects() function for inferring better data types.

Conversion Function in Pandas Python for Data Science - PST Analytics

OUTPUT:

Conversion Function in Pandas Python for Data Science - PST Analytics

Now we will see the dtype of each column in dataframe.

Conversion Function in Pandas Python for Data Science - PST Analytics

We can see from the above output that first and third column are of object type and second column is of int64 type. Now we will slice the datarame and create a new dataframe from the slice.

OUTPUT:

Now we will see the infer_objects() function.

OUTPUT:

Detecting missing values:

DataFrame.isna() is a function there for detecting missing values. It will return a Boolean same-sized object which will indicate if the values are NA or not. The NA values like None or numpy.NaN gets mapped to True values. All other things get mapped to False values. Characters like empty strings or numpy.inf are not considered to be NA values.

Code 1: We use isna() function for detecting the missing values in a dataframe.

Conversion Function in Pandas Python for Data Science - PST Analytics

Now we will use the isna() function for detecting the missing values.

OUTPUT:

In output the cells corresponding to the missing values contain true values; otherwise, it gives false.

Detecting existing or non-missing values:

DataFrame.notna() is a function which detects existing or non-missing values in the dataframe. The function will return a Boolean object having the same size as that of the object on which it is applied. It indicates whether each individual value is a na value or not. All of the missing values are mapped true, and the missing values gets mapped to false.

Code 1: We use the notna() function for finding all the non-missing values in the dataframe.

Now we will use the function dataframe.notna() function for finding all the non-missing values in the dataframe.

OUTPUT:

Conversion Function in Pandas Python for Data Science - PST Analytics

Methods for conversion in DataFrame:

Conversion Function in Pandas Python for Data Science - PST Analytics 

So, to learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.