Hello, readers! In this article, we will be focusing on 4 Important Pandas Conversion functions in Python.
So, let us get started!
The 4 Pandas Conversion Functions
In the course of this topic, we will be making use of the Bike Rental Count Prediction dataset in the examples. You can find the dataset here.
1. Pandas astype() function
The most prominently used Pandas conversion functions available in Pandas module is astype() function.
With astype() function, we can easily convert the data type of the variables from one type to another at ease. Thus, the inter-conversion between the data variables becomes easy.
In this example, initially, we check the data types of the variables of dataset using dtypes object.
Data type of the variables:
instant int64 dteday object season int64 yr int64 mnth int64 holiday int64 weekday int64 workingday int64 weathersit int64 temp float64 atemp float64 hum float64 windspeed float64 casual int64 registered int64 cnt int64 dtype: object
Further to which, we now attempt to change the data type of the variable season from integer to category using astype() function.
Conversion of integer to category–
BIKE.season = BIKE.season.astype("category") BIKE.dtypes
instant int64 dteday object season category yr int64 mnth int64 holiday int64 weekday int64 workingday int64 weathersit int64 temp float64 atemp float64 hum float64 windspeed float64 casual int64 registered int64 cnt int64
2. Pandas isna() function
When it comes to data analysis and pre-processing, it is very important for us to study the data and draw out meaningful information. Checking the presence of NULL values seems to be an important step prior to modeling and conversions.
With isna() function, we can easily check for the presence of NULL values, if any. It is a boolean function that returns TRUE, if it detects any NULL value within the column or dataset.
In this example, the isna() function returns false because it encounters zero missing or NULL values in the dataset.
3. Python notna() function
With Python notna() function, we can easily segregate and look up for the values which are not equivalent to NULL or NA. That is, the notna() function returns TRUE, if it detects the presence of a non-null value.
In the below example, the notna() function returns TRUE because it does not detect any null values throughout the dataset.
4. Python dataframe.copy() function
When it comes to manipulating a dataset, it is very crucial for us to have a backup of the original data as a spare copy to make changes to. This also helps us to roll back to the original situation in case the manipulations do not work out.
In this example, we have created dataframe ‘df’ that holds the copy of the dataset represented by BIKE object using copy() function.
df = BIKE.copy() print(df)
By this, we have come to the end of this topic on Python conversion functions. Feel free to comment below, in case you come across any question.
For more such posts related to Python Programming, Stay tuned with us!
Till then, Happy Learning! 🙂