phpcms 外贸网站模板,上海app开发制作,安徽省建设工程安全 协会网站,wordpress dosortcode文章目录明确指定数据的类型通过dtypes属性进行查看创建Pandas对象指定数据类型转换数据类型通过astype()方法强制转换数据的类型通过to_numeric()函数转换数据类型明确指定数据的类型
通过dtypes属性进行查看
import pandas as pddf pd.DataFrame({A: [1, 2, 4],B: [9, -80…
文章目录明确指定数据的类型通过dtypes属性进行查看创建Pandas对象指定数据类型转换数据类型通过astype()方法强制转换数据的类型通过to_numeric()函数转换数据类型明确指定数据的类型
通过dtypes属性进行查看
import pandas as pddf 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]},dtypeint)
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, copyTrue, 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(dtypefloat))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(errorsignore):\n, df[C].astype(dtypefloat, errorsignore))输出结果
df[C]:0 x
1 5.9
2 0
Name: C, dtype: object
df[C].astype(errorsignore):0 x
1 5.9
2 0
Name: C, dtype: object通过to_numeric()函数转换数据类型
to_numeric()函数不能直接操作DataFrame对象 pandas.to_numeric(arg, errors‘raise’, downcastNone) 上述函数中常用参数表示的含义如下
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], errorsignore):\n, pd.to_numeric(se2, errorsignore))
print(to_numeric(df[C], errorscoerce):\n, pd.to_numeric(se2, errorscoerce))输出结果
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], errorsignore):0 x
1 5.9
2 0
Name: C, dtype: object
to_numeric(df[C], errorscoerce):0 NaN
1 5.9
2 0.0
Name: C, dtype: float64