Sunday 29 October 2023

Pandas: Convert column type of a DataFrame

Using ‘asType’ method, we can change the column type of a DataFrame.

Example

df['Age'] = df['Age'].astype('int')

 

Above snippet change the column type of ‘Age’ to int, It works only when the column Age do not have missing values.

 

change_column_type.py

import pandas as pd
import numpy as np

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Ram', 'Joel', 'Gopi', 'Jitendra', "Raj"],
        'Age': [34, np.nan, 29, 41, 52, np.nan],
        'City': ['Bangalore', None, 'Hyderabad', None, 'Bangalore', 'Chennai']}

df = pd.DataFrame(data)
print(df)

print('\nGet the DataFrame information')
df.info()

if df['Age'].hasnans == True:
    print('\nFound missing values, replacing them with 0')
    df['Age'].fillna(0, inplace=True)

print('\nSet the type of "age" column as int\n')
df['Age'].astype('int')
df.info()

 

Output

       Name   Age       City
0   Krishna  34.0  Bangalore
1       Ram   NaN       None
2      Joel  29.0  Hyderabad
3      Gopi  41.0       None
4  Jitendra  52.0  Bangalore
5       Raj   NaN    Chennai

Get the DataFrame information
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Name    6 non-null      object 
 1   Age     4 non-null      float64
 2   City    4 non-null      object 
dtypes: float64(1), object(2)
memory usage: 272.0+ bytes

Found missing values, replacing them with 0

Set the type of "age" column as int

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   Name    6 non-null      object
 1   Age     6 non-null      int64 
 2   City    4 non-null      object
dtypes: int64(1), object(2)
memory usage: 272.0+ bytes

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment