How to change the data type of a column in an array or a DataFrame

Published on Aug. 22, 2023, 12:16 p.m.

In both NumPy and Pandas, you can use the astype() function to change the data type of a column in an array or a DataFrame.

Here’s an example of how to change the data type of a column in a NumPy array:

import numpy as np

# create a 2D NumPy array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# change the data type of the second column to float
arr[:, 1] = arr[:, 1].astype(float)

# check the data type of the array
print(arr.dtype)

In this example, we first create a 2D NumPy array, and then use astype() to change the data type of the second column to float. Finally, we check the data type of the array.

Here’s an example of how to change the data type of a column in a Pandas DataFrame:

import pandas as pd

# create a Pandas DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['4', '5', '6']})

# change the data type of column 'B' to int
df['B'] = df['B'].astype(int)

# check the data type of the DataFrame
print(df.dtypes)

In this example, we first create a Pandas DataFrame, and then use astype() to change the data type of column ‘B’ to int. Finally, we check the data type of the DataFrame.

Tags: