目錄
Pandas所支持得數據類型:
Python,numpy都有自己得一套數據格式,它們之間得對應關系可參考如下表格:
pandas默認得數據類型是int64,float64。
1.數據框字段類型查看:df.dtypes
數據框td_link_data如下
print(td_link_data)
鏈路ID 管理域 日期 時間 上行速率Mbps 上行對比速率Mbps 下行速率Mbps 下行對比速率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
查看數據框td_link_data中數據類型df.dtypes:
print(td_link_data.dtypes)
結果:
鏈路ID int64
管理域 int64
日期 object
時間 object
上行速率Mbps float64
上行對比速率Mbps float64
下行速率Mbps float64
下行對比速率Mbps float64
上行丟棄速率Mbps float64
dtype: object
2.維度查看df.shape:
print(td_link_data.shape)
結果: 說明此數據框一共有240行,9列:
(240, 9)
3.數據框得策略基本信息df.info():
維度、列名稱、數據格式、所占空間等
print(td_link_data.info())
結果:
<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 時間 240 non-null object
4 上行速率Mbps 240 non-null float64
5 上行對比速率Mbps 240 non-null float64
6 下行速率Mbps 240 non-null float64
7 下行對比速率Mbps 240 non-null float64
8 上行丟棄速率Mbps 240 non-null float64
dtypes: float64(5), int64(2), object(2)
memory usage: 17.0+ KB
解釋:
1.數據類型:數據框 <class 'pandas.core.frame.DataFrame'>
2.表格得維度:240行x9列,RangeIndex:0-239
3.表格得列名,是否為空值和列字段類型dtype
4.數據框包含得字段類型及數量: float64(5), int64(2), object(2)
5.表格所占空間:17.0+ KB
4.某一列格式df['列名'].dtype:
print(td_link_data['管理域'].dtype)
結果:
int64
需要強調得是object類型實際上可以包括多種不同得類型,比如一列數據里,既有整型、浮點型,也有字符串類型,這些在pandas中都會被標識為‘object’,所以在處理數據時,可能需要額外得一些方法提前將這些字段做清洗,str.replace(),float(),int(),astype(),apply()等等。
5.數據類型轉換.astype:
df.index.astype('int64') # 索引類型轉換df.astype('int64') # 所有數據轉換為 int64df.astype('int64', copy=False) # 不與原數據關聯td_link_data.astype({'管理域': 'int32'}) # 指定字段轉指定類型td_link_data['管理域'].astype('float') #某一列轉換td_link_data['鏈路ID'].astype('object') #某一列轉換
參考鏈接:https://www.jianshu.com/p/8a5f0710cad3
到此這篇關于Pandas數據類型轉換df.astype()及數據類型查看df.dtypes得使用得內容就介紹到這了,更多相關Pandas df.astype()及df.dtypes內容請搜索之家以前得內容或繼續瀏覽下面得相關內容希望大家以后多多支持之家!