Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Pandas Introduction

Pandas Series

Pandas DataFrame

Pandas Read Files

Pandas Some functions and properties

Pandas Math Function

Pandas Selection

Pandas Change Type

Pandas Concatenate & Split

Pandas Sorting

Pandas Filter

Pandas Data Cleaning

Pandas Group by

Pandas Time Series

Pandas Analysis 1

Pandas Analysis 2

Pandas Analysis 3

Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Change data type using pandas

Let's see the dataset. This dataset is will be used in upcoming example of code .

Input
import pandas as pd
df=pd.read_csv("practice.csv")
print(df)
Output
Id Name Group_name Total_marks Grade Ranking
0 01 A Science 700 A+ 01
1 02 B Commerce 618 B+ 02
2 03 A Science 700 A+ 01
3 04 D Arts 687 A+ 01
4 05 E Commerce 611 B+ 02
5 06 F Arts 599 C+ 03
6 07 P Science 575 C+ 03
7 08 F Arts 600 C 03
8 09 I Commerce 550 C+ 03
9 10 J Science 650 A+ 01
10 11 K Arts 680 A+ 01
11 12 L Science 570 C+ 03
12 13 M Arts 599 C+ 03
13 14 N Commerce 597 C+ 03
14 15 O Science 697 A+ 01
15 16 B Arts 570 C+ 03
16 17 D Science 588 C+ 03
17 18 E Science 687 A+ 01
18 19 C Commerce 688 A+ 01
19 20 P Arts 588 C+ 03
20 21 C Science 619 B+ 02
21 22 M Commerce 600 B+ 02
22 23 P Arts 700 A+ 01


astype function()

Astype function is used to data type conversion like float to int or string, string to float or int and int to float or string.

Input
import pandas as pd
df=pd.read_csv("practice.csv")

Print("Original Dtypes")
print(df.dtypes)

df["Id"]=df["Id"].astype(float)
df["Ranking"]=df["Ranking"].astype(int)
df["Total marks"]=df["Total marks"].astype(str)
df['Date'] = df['Date'].astype('datetime64[ns]')

print("Dtype after conversion")
print("New Id column",df["Id"].dtypes)
print("New Ranking column",df["Ranking"].dtypes)
print("New Total marks column",df["Id"].dtypes)
Output
Original Dtypes
Id    int64
Name    object
Group_name    object
Total marks    int64
Grade    object
Ranking    int64
Date    object
dtype: object

Dtype after conversion

New Id column float64
New Ranking column int32
New Total marks column float64

Float to integer tricks

To convert float to integer, round those float values before converting into integer.

Input
import pandas as pd
df=pd.read_csv("prac.csv")
print(df)
df["Ranking"]=df["Ranking"].round(0).astype(int)
print("New Ranking column",df["Ranking"].dtypes)
Output
Id Money
0 1.12 $4,334.00
1 2.31 $4,343.00
2 3.00 $4,352.00
3 4.11 $4,361.00
4 5.01 $4,370.00
5 6.21 $4,379.00


to_numeric function()

If you have string in numeric column then you can't convert that column into integer or float using astype. So you have to use to_numeric function and use an extra parameter and that is errors="coerce". When this parameter gets a string it will make that cell nan. After doing this you will get a numeric column and to change it into float or int you can use astype function.

Input
import pandas as pd
df=pd.read_csv("prac.csv")
df["Ranking"]=pd.to_numeric(df["Ranking"],errors="coerce").astype(int)
print("New Ranking column",df["Ranking"].dtypes)

Suppose you have some symbol(dollar, comma, etc) with numeric values. You can't convert those values into numeric from with those symbols. For the conversion, you have to remove those symbols. After removing the symbol you can change those values into float or integer.

Input
import pandas as pd
df=pd.read_csv("prac.csv")
print(df)
df["money"]=df["money"].str.replace("$","").str.replace(",","")
print(df)
df["money"].astype(float)
print("After changing dtypes",df["money"].dtypes)
Output
Id Money
0 01 $4,334.00
1 02 $4,343.00
2 03 $4,352.00
3 04 $4,361.00
4 05 $4,370.00
5 06 $4,379.00


Name: money, dtype: object

Id Money
0 01 4334.0
1 02 4343.0
2 03 4352.0
3 04 4361.0
4 05 4370.0
5 06 4379.0

Change date-time column data type

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.