深度阅读

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

作者
作者
2023年08月22日
更新时间
11.45 分钟
阅读时间
0
阅读量

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.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。