pandas中查看數(shù)據(jù)類型的幾種方式

本文系轉(zhuǎn)載,在原文基礎上做了修改
作者:精靈鼠小弟_fb22
鏈接:http://www.itdecent.cn/p/8a5f0710cad3
來源:簡書
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

在獲得數(shù)據(jù)之后、分析數(shù)據(jù)之前,我們一般需要對數(shù)據(jù)總體進行一個概覽,如有哪些字段,每個字段的類型,值是否缺失等,以下列出了幾種方法,供我們方便快捷的查看dataframe的數(shù)據(jù)類型。

1、維度查看:df.shape

# 以某表為例,先讀取源數(shù)據(jù),不同城市,每天的產(chǎn)品銷售信息
engine = create_engine('mysql+pymysql://username:password@localjost:3306/dataset?charset=utf8')
sql = 'select * from dw_customer_order'
gather_customer_order = pd.read_sql_query(sql,con=engine)
gather_customer_order.shape

返回結(jié)果如下如所示,說明此表格一共有203401行,16列:

(203401, 16)

2、數(shù)據(jù)表基本信息(維度、列名稱、數(shù)據(jù)格式、所占空間等):df.info()

gather_customer_order.info()

返回結(jié)果

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 203401 entries, 0 to 203400
Data columns (total 16 columns):
#   Column              Non-Null Count   Dtype  
---  ------              --------------   -----  
0   create_date         203401 non-null  object 
1   product_name        203401 non-null  object 
2   cpzl_zw             203401 non-null  object 
3   cplb_zw             203401 non-null  object 
4   order_num           203401 non-null  int64  
5   customer_num        203401 non-null  int64  
6   sum_amount          203401 non-null  float64
7   is_current_year     203401 non-null  object 
8   is_last_year        203401 non-null  object 
9   is_yesterday        203401 non-null  object 
10  is_today            203401 non-null  object 
11  is_current_month    203401 non-null  object 
12  is_current_quarter  203401 non-null  object 
13  chinese_province    203401 non-null  object 
14  chinese_city        203401 non-null  object 
15  chinese_territory   203401 non-null  object 
dtypes: float64(1), int64(2), object(13)
memory usage: 24.8+ MB

可見,用info方法可以非常全面的看出表格的各項屬性,包括:
1.源數(shù)據(jù)的數(shù)據(jù)類型為:DataFrame
2.表格的維度:203401行 * 16列,RangeIndex:0-203400
3.表格的列名,是否為空值和列字段類型dtype
4.表格所占空間:24.8M+

3、每一列數(shù)據(jù)的格式:df.dtypes
這個功能與df.info()類似,如果只想查看每一列存儲的是什么數(shù)據(jù)格式,那么可以直接使用df.dtypes

gather_customer_order.dtypes

返回結(jié)果如下,可以看到,這個結(jié)果基本就是df.info()的簡化版,指明了各列的數(shù)據(jù)類型。

create_date            object
product_name           object
cpzl_zw                object
cplb_zw                object
order_num               int64
customer_num            int64
sum_amount            float64
is_current_year        object
is_last_year           object
is_yesterday           object
is_today               object
is_current_month       object
is_current_quarter     object
chinese_province       object
chinese_city           object
chinese_territory      object
dtype: object

4、某一列格式:df['B'].dtype
5.type(變量名)
通過以上4種方式有時候也無法看到數(shù)據(jù)類型,比如下圖中,通過df.dtypes查每一列的數(shù)據(jù)類型,字段'create_date'是'object'(這里表示對象的意思),那么這個對象到底是什么類型不知道,就需要我們進一步查詢確定了,這時可以使用type(df['列名'])把維度降下來查其類型?;蛘咧苯邮褂胠oc[]函數(shù)降維度(可以不斷使用loc[]函數(shù)降維度),查其數(shù)據(jù)類型:type(df['列名'].loc[])

image.png

image.png

比如要想要獲取上圖中‘create_date’字段中的年月,首先我們得查看該字段的數(shù)據(jù)類型

type(gather_customer_order['create_date'])

結(jié)果如下:

pandas.core.series.Series

可以看出是Series類型,Series類型可以使用loc[]函數(shù)來進行切片,再去針對切片查看數(shù)據(jù)類型

type(gather_customer_order['create_date'].loc[0])  # 查看'create_date'字段的第一個值的類型

結(jié)果如下:是datetime類型

datetime.date

由上文可見,float64,int64,object都是pandas專有的數(shù)據(jù)格式,同理,Python,numpy都有自己的一套數(shù)據(jù)格式,它們之間的對應關(guān)系可參考下面的表格:


image.png

這里需要強調(diào)的是object類型實際上可以包括多種不同的類型,比如一列數(shù)據(jù)里,既有整型、浮點型,也有字符串類型,這些在pandas中都會被標識為‘object’,所以在處理數(shù)據(jù)時,可能需要額外的一些方法提前將這些字段做清洗,str.replace(),float(),int(),astype(),apply()等等。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容