一个例子辨析python中type、dtype、astype的区别

函数 说明
type() 返回数据结构类型
dtype() 返回数据元素的数据类型
astype() 转换所有数据元素的数据类型
#Jupyter python3
import numpy as np
import pandas as pd
datas=np.random.randn(3,3)
index=['A','B','C']
columns=[0,1,2]
df=pd.DataFrame(datas,index=index,columns=columns)
df1=np.array(df)

#type()查看数据类型
print(type(df1))
#

#dtype()
df1.dtype
#dtype('float64')


#astype()
df2=df1.astype(int)
df2.dtype#转换后输出元素的数据类型
#dtype('int32')

print(type(df2))#只改变元素的数据类型,数据结构类型不改变
#


欢迎关注个人公众号:茶哩茶

你可能感兴趣的:(python进阶)