目錄
- 1.數(shù)據(jù)框字段類型查看:df.dtypes
- 2.維度查看df.shape:
- 3.數(shù)據(jù)框得策略基本信息df.info():
- 4.某一列格式df['列名'].dtype:
- 5.數(shù)據(jù)類型轉(zhuǎn)換.astype:
Pandas所支持得數(shù)據(jù)類型:
Python,numpy都有自己得一套數(shù)據(jù)格式,它們之間得對(duì)應(yīng)關(guān)系可參考如下表格:
pandas默認(rèn)得數(shù)據(jù)類型是int64,float64。
1.數(shù)據(jù)框字段類型查看:df.dtypes
數(shù)據(jù)框td_link_data如下
print(td_link_data)
鏈路ID 管理域 日期 時(shí)間 上行速率Mbps 上行對(duì)比速率Mbps 下行速率Mbps 下行對(duì)比速率Mbps 上行丟棄速率Mbps
0 500 10001 20210609 10 0.000 0.011 0.000 0.001 0.0
1 500 10001 20210609 11 0.000 0.007 0.000 0.000 0.0
2 500 10001 20210609 12 0.000 0.028 0.000 0.002 0.0
3 500 10001 20210609 13 0.000 0.056 0.000 0.003 0.0
4 500 10001 20210609 14 0.000 0.062 0.000 0.003 0.0
5 500 10001 20210609 15 0.000 0.074 0.000 0.005 0.0
6 500 10001 20210609 16 0.000 0.061 0.000 0.004 0.0
7 500 10001 20210609 17 0.000 0.069 0.000 0.004 0.0
8 500 10001 20210609 18 0.000 0.054 0.000 0.002 0.0
9 500 10001 20210609 19 0.000 0.054 0.000 0.002 0.0
10 500 10001 20210609 20 0.000 0.040 0.000 0.004 0.0
... ... ... ... ... ... ... ... ... ...
... ... ... ... ... ... ... ... ... ...
... ... ... ... ... ... ... ... ... ...
239 500 10001 20210609 23 0.000 0.040 0.000 0.004 0.0
查看數(shù)據(jù)框td_link_data中數(shù)據(jù)類型df.dtypes:
print(td_link_data.dtypes)
結(jié)果:
鏈路ID int64
管理域 int64
日期 object
時(shí)間 object
上行速率Mbps float64
上行對(duì)比速率Mbps float64
下行速率Mbps float64
下行對(duì)比速率Mbps float64
上行丟棄速率Mbps float64
dtype: object
2.維度查看df.shape:
print(td_link_data.shape)
結(jié)果: 說明此數(shù)據(jù)框一共有240行,9列:
(240, 9)
3.數(shù)據(jù)框得策略基本信息df.info():
維度、列名稱、數(shù)據(jù)格式、所占空間等
print(td_link_data.info())
結(jié)果:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 240 entries, 0 to 239
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 鏈路ID 240 non-null int64
1 管理域 240 non-null int64
2 日期 240 non-null object
3 時(shí)間 240 non-null object
4 上行速率Mbps 240 non-null float64
5 上行對(duì)比速率Mbps 240 non-null float64
6 下行速率Mbps 240 non-null float64
7 下行對(duì)比速率Mbps 240 non-null float64
8 上行丟棄速率Mbps 240 non-null float64
dtypes: float64(5), int64(2), object(2)
memory usage: 17.0+ KB
解釋:
1.數(shù)據(jù)類型:數(shù)據(jù)框 <class 'pandas.core.frame.DataFrame'>
2.表格得維度:240行x9列,RangeIndex:0-239
3.表格得列名,是否為空值和列字段類型dtype
4.數(shù)據(jù)框包含得字段類型及數(shù)量: float64(5), int64(2), object(2)
5.表格所占空間:17.0+ KB
4.某一列格式df['列名'].dtype:
print(td_link_data['管理域'].dtype)
結(jié)果:
int64
需要強(qiáng)調(diào)得是object類型實(shí)際上可以包括多種不同得類型,比如一列數(shù)據(jù)里,既有整型、浮點(diǎn)型,也有字符串類型,這些在pandas中都會(huì)被標(biāo)識(shí)為‘object’,所以在處理數(shù)據(jù)時(shí),可能需要額外得一些方法提前將這些字段做清洗,str.replace(),float(),int(),astype(),apply()等等。
5.數(shù)據(jù)類型轉(zhuǎn)換.astype:
df.index.astype('int64') # 索引類型轉(zhuǎn)換df.astype('int64') # 所有數(shù)據(jù)轉(zhuǎn)換為 int64df.astype('int64', copy=False) # 不與原數(shù)據(jù)關(guān)聯(lián)td_link_data.astype({'管理域': 'int32'}) # 指定字段轉(zhuǎn)指定類型td_link_data['管理域'].astype('float') #某一列轉(zhuǎn)換td_link_data['鏈路ID'].astype('object') #某一列轉(zhuǎn)換
參考鏈接:https://www.jianshu.com/p/8a5f0710cad3
到此這篇關(guān)于Pandas數(shù)據(jù)類型轉(zhuǎn)換df.astype()及數(shù)據(jù)類型查看df.dtypes得使用得內(nèi)容就介紹到這了,更多相關(guān)Pandas df.astype()及df.dtypes內(nèi)容請(qǐng)搜索之家以前得內(nèi)容或繼續(xù)瀏覽下面得相關(guān)內(nèi)容希望大家以后多多支持之家!