Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a program in Python to covert the datatype of a particular column in a dataframe
In Pandas, you can convert the datatype of a specific column using the astype() method. This is useful when you need to change data types for analysis, memory optimization, or data consistency.
Creating a Sample DataFrame
Let's start by creating a DataFrame with mixed data types ?
import pandas as pd
data = {'Name': ['David', 'Adam', 'Bob', 'Alex', 'Serina'],
'Age': [13, 12, 12, 13, 12],
'Maths': [98, 59, 66, 95, 70],
'Science': [75, 96, 55, 49, 78],
'English': [79, 45, 70, 60, 80],
'Result': [8.1, 6.2, 6.3, 7.2, 8.3]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
print("\nData types:")
print(df.dtypes)
Original DataFrame:
Name Age Maths Science English Result
0 David 13 98 75 79 8.1
1 Adam 12 59 96 45 6.2
2 Bob 12 66 55 70 6.3
3 Alex 13 95 49 60 7.2
4 Serina 12 70 78 80 8.3
Data types:
Name object
Age int64
Maths int64
Science int64
English int64
Result float64
dtype: object
Converting Float to Integer
To convert the 'Result' column from float64 to int64, use the astype() method ?
import pandas as pd
data = {'Name': ['David', 'Adam', 'Bob', 'Alex', 'Serina'],
'Age': [13, 12, 12, 13, 12],
'Maths': [98, 59, 66, 95, 70],
'Science': [75, 96, 55, 49, 78],
'English': [79, 45, 70, 60, 80],
'Result': [8.1, 6.2, 6.3, 7.2, 8.3]}
df = pd.DataFrame(data)
print("Before conversion:")
print(df.dtypes)
# Convert Result column from float to int
df['Result'] = df['Result'].astype(int)
print("\nAfter conversion:")
print(df.dtypes)
print("\nConverted DataFrame:")
print(df)
Before conversion:
Name object
Age int64
Maths int64
Science int64
English int64
Result float64
dtype: object
After conversion:
Name object
Age int64
Maths int64
Science int64
English int64
Result int64
dtype: object
Converted DataFrame:
Name Age Maths Science English Result
0 David 13 98 75 79 8
1 Adam 12 59 96 45 6
2 Bob 12 66 55 70 6
3 Alex 13 95 49 60 7
4 Serina 12 70 78 80 8
Alternative Methods
You can also convert multiple columns at once using a dictionary ?
import pandas as pd
data = {'Name': ['David', 'Adam', 'Bob'],
'Score': [85.5, 92.3, 78.8],
'Grade': [3.2, 3.8, 2.9]}
df = pd.DataFrame(data)
print("Before conversion:")
print(df.dtypes)
# Convert multiple columns at once
df = df.astype({'Score': 'int', 'Grade': 'int'})
print("\nAfter conversion:")
print(df.dtypes)
print("\nConverted DataFrame:")
print(df)
Before conversion:
Name object
Score float64
Grade float64
dtype: object
After conversion:
Name object
Score int64
Grade int64
dtype: object
Converted DataFrame:
Name Score Grade
0 David 85 3
1 Adam 92 3
2 Bob 78 2
Common Data Type Conversions
| Original Type | Target Type | Method |
|---|---|---|
| float64 | int64 | astype(int) |
| int64 | str | astype(str) |
| object | float64 | astype(float) |
| object | datetime | pd.to_datetime() |
Conclusion
Use astype() to convert column data types in Pandas DataFrames. This method is essential for data cleaning and ensures your data has the correct types for analysis.
Advertisements
