Python更改数据类型——astype()方法和to_numeric()函数

本文围绕Pandas数据类型展开,介绍了明确指定数据类型的方法,可通过dtypes属性查看,创建Pandas对象时也能指定。还阐述了转换数据类型的方式,如用astype()方法强制转换,以及to_numeric()函数转换,后者能解决前者在处理含非数字字符数据时的局限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


明确指定数据的类型

通过dtypes属性进行查看

import pandas as pd

df = pd.DataFrame({'A': ['1', '2', '4'],
                   'B': ['9', '-80', '5.3'],
                   'C': ['x', '5.9', '0']})
print("df.dtypes:\n", df.dtypes)
print("df:\n", df)

输出结果:

df.dtypes:
 A    object
B    object
C    object
dtype: object
df:
    A    B    C
0  1    9    x
1  2  -80  5.9
2  4  5.3    0

创建Pandas对象指定数据类型

data = pd.DataFrame({'A': ['1', '2', '4'],
                     'B': ['9', '80', '5']},
                    dtype='int')
print("data:\n", data)
print("data.dtypes:\n", data.dtypes)

输出结果:

data:
    A   B
0  1   9
1  2  80
2  4   5
data.dtypes:
 A    int32
B    int32
dtype: object

转换数据类型

通过astype()方法强制转换数据的类型

astype(dypte, copy=True, errors = ‘raise’, **kwargs)

上述方法中部分参数表示的含义如下:

dtype:表示数据类型

copy:是否建立副本,默认为True

errors:错误采取的处理方式,可以取值为raise或ignore,默认为raise。其中raise表示允许引发异常,ignore表示抑制异常。


运用astype()方法将DataFrame对象df中B列数据的类型转换为int类型:

print("df['B']:\n", df['B'])
print("df['B'].astype:\n", df['B'].astype(dtype='float'))
df['B']:
 0      9
1    -80
2    5.3
Name: B, dtype: object
df['B'].astype:
 0     9.0
1   -80.0
2     5.3
Name: B, dtype: float64

之所以没有将所有列进行类型转换是因为C列中有非数字类型的字符,无法将其转换为int类型,若强制转换会出现ValueError异常。(当参数errors取值ignore时可以抑制异常,但抑制异常后输出结果仍是未转换类型之前的对象——也就是并未进行数据类型转换的操作,只是不会报错罢了

print("df['C']:\n", df['C'])
print("df['C'].astype(errors='ignore'):\n", df['C'].astype(dtype='float', errors='ignore'))

输出结果:

df['C']:
 0      x
1    5.9
2      0
Name: C, dtype: object
df['C'].astype(errors='ignore'):
 0      x
1    5.9
2      0
Name: C, dtype: object

通过to_numeric()函数转换数据类型

to_numeric()函数不能直接操作DataFrame对象

pandas.to_numeric(arg, errors=‘raise’, downcast=None)

上述函数中常用参数表示的含义如下:

arg:表示要转换的数据,可以是list、tuple、Series

errors:错误采用的处理方式可以取值除raise、ignore外,还可以取值coerce,默认为raise。其中raise表示允许引发异常,ignore表示抑制异常。

to_numeric()函数较之astype()方法的优势在于解决了后者的局限性:只要待转换的数据中存在数字以外的字符,在使用后者进行类型转换时就会出现错误,而to_numeric()函数之所以可以解决这个问题,就源于其errors参数可以取值coerce——当出现非数字字符时,会将其替换为缺失值之后进行数据类型转换。


se = pd.Series(df['A'])
se1 = pd.Series(df['B'])
se2 = pd.Series(df['C'])
print("df['A']:\n", df['A'])
print("to_numeric(df['A']):\n", pd.to_numeric(se))
print("df['B']:\n", df['B'])
print("to_numeric(df['B']):\n", pd.to_numeric(se1))
print("df['C']:\n", df['C'])
print("to_numeric(df['C'], errors='ignore'):\n", pd.to_numeric(se2, errors='ignore'))
print("to_numeric(df['C'], errors='coerce'):\n", pd.to_numeric(se2, errors='coerce'))

输出结果:

df['A']:
 0    1
1    2
2    4
Name: A, dtype: object
to_numeric(df['A']):
 0    1
1    2
2    4
Name: A, dtype: int64
df['B']:
 0      9
1    -80
2    5.3
Name: B, dtype: object
to_numeric(df['B']):
 0     9.0
1   -80.0
2     5.3
Name: B, dtype: float64
df['C']:
 0      x
1    5.9
2      0
Name: C, dtype: object
to_numeric(df['C'], errors='ignore'):
 0      x
1    5.9
2      0
Name: C, dtype: object
to_numeric(df['C'], errors='coerce'):
 0    NaN
1    5.9
2    0.0
Name: C, dtype: float64
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

·Jormungand

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值