最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談python 中的 type(), dtype(), astype()的區(qū)別

 更新時間:2020年04月09日 15:03:16   作者:wzg2016  
這篇文章主要介紹了淺談python 中的 type(), dtype(), astype()的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

函數(shù) 說明
type() 返回數(shù)據(jù)結(jié)構(gòu)類型(list、dict、numpy.ndarray 等)
dtype()

返回數(shù)據(jù)元素的數(shù)據(jù)類型(int、float等)

備注:1)由于 list、dict 等可以包含不同的數(shù)據(jù)類型,因此不可調(diào)用dtype()函數(shù)

2)np.array 中要求所有元素屬于同一數(shù)據(jù)類型,因此可調(diào)用dtype()函數(shù)

astype()

改變np.array中所有數(shù)據(jù)元素的數(shù)據(jù)類型。

備注:能用dtype() 才能用 astype()

測試代碼:

import numpy as np
class Myclass():
 pass
 
a = [[1,2,3],[4,5,6]]
b = {'a':1,'b':2,'c':3}
c = np.array([1,2,3])
d = Myclass()
e = np.linspace(1,5,10)
c_ = c.astype(np.float)
f = 10
 
print("type(a)=",type(a))
print("type(b)=",type(b))
print("type(c)=",type(c))
print("type(d)=",type(d))
print("type(e)=",type(e))
print("type(f)=",type(f))
print("type(c_)=",type(c_))
 
# print(a.dtype) ## AttributeError: 'list' object has no attribute 'dtype'
# print(b.dtype) ## AttributeError: 'dict' object has no attribute 'dtype'
print(c.dtype)
# print(d.dtype) ## AttributeError: 'Myclass' object has no attribute 'dtype'
print(e.dtype)
print(c_.dtype)
# print(f.dtype) ## AttributeError: 'int' object has no attribute 'dtype'
 
# print(a.astype(np.int)) ## AttributeError: 'list' object has no attribute 'astype'
# print(b.astype(np.int)) ## AttributeError: 'dict' object has no attribute 'astype'
print(c.astype(np.int))
# print(d.astype(np.int)) ## AttributeError: 'Myclass' object has no attribute 'astype'
print(e.astype(np.int))
# print(f.astype(np.int)) ## AttributeError: 'int' object has no attribute 'astype'

補充知識:pandas astype()錯誤

由于數(shù)據(jù)出現(xiàn)錯誤

DataError: No numeric types to aggregate

改正以后才認識到astype的重要性。

Top15['populations'] = Top15['Energy Supply'].div(Top15['Energy Supply per Capita']).astype(float)
df_mean = ((df.set_index('Continent').groupby(level=0)['populations'].agg({'mean' : np.mean})))
#加了astype(float)后無錯誤

以上這篇淺談python 中的 type(), dtype(), astype()的區(qū)別就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pycharm配置python環(huán)境的詳細圖文教程

    pycharm配置python環(huán)境的詳細圖文教程

    PyCharm是一款功能強大的Python編輯器,具有跨平臺性,下面這篇文章主要給大家介紹了關(guān)于pycharm配置python環(huán)境的詳細圖文教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • Python連接和操作Elasticsearch的詳細指南

    Python連接和操作Elasticsearch的詳細指南

    Elasticsearch 是一個強大的搜索引擎,廣泛應(yīng)用于數(shù)據(jù)存儲和搜索場景,通過 Python,我們可以方便地與 Elasticsearch 進行交互,本文將詳細介紹如何在本地使用 Python 連接到服務(wù)器上的 Elasticsearch,并進行基本的操作,需要的朋友可以參考下
    2024-12-12
  • windows 10下安裝搭建django1.10.3和Apache2.4的方法

    windows 10下安裝搭建django1.10.3和Apache2.4的方法

    最近發(fā)現(xiàn)很多教程都是在linux上搭建,windows上似乎天生不太適合,但是我還是愿意試試這個坑。下面這篇文章主要給大家介紹了在windows 10系統(tǒng)下安裝搭建django1.10.3和Apache2.4的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • 淺談tensorflow與pytorch的相互轉(zhuǎn)換

    淺談tensorflow與pytorch的相互轉(zhuǎn)換

    本文主要介紹了簡單介紹一下tensorflow與pytorch的相互轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • python3 打印輸出字典中特定的某個key的方法示例

    python3 打印輸出字典中特定的某個key的方法示例

    這篇文章主要介紹了python3 打印輸出字典中特定的某個key的方法,涉及Python字典的遍歷、判斷、輸出等相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • python讀寫二進制文件的方法

    python讀寫二進制文件的方法

    這篇文章主要介紹了python讀寫二進制文件的方法,實例分析了Python讀寫二進制文件的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Python3去除頭尾指定字符的函數(shù)strip()、lstrip()、rstrip()用法詳解

    Python3去除頭尾指定字符的函數(shù)strip()、lstrip()、rstrip()用法詳解

    這篇文章主要介紹了Python3去除頭尾指定字符的函數(shù)strip()、lstrip()、rstrip()用法詳解,需要的朋友可以參考下
    2021-04-04
  • pymysql的安裝以及操作實戰(zhàn)指南

    pymysql的安裝以及操作實戰(zhàn)指南

    這篇文章主要給大家介紹了關(guān)于pymysql的安裝以及操作的相關(guān)資料,pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • Python圖像處理模塊ndimage用法實例分析

    Python圖像處理模塊ndimage用法實例分析

    這篇文章主要介紹了Python圖像處理模塊ndimage用法,結(jié)合實例形式分析了Python圖像處理模塊ndimage基本功能及常見的圖形運算操作實現(xiàn)技巧,需要的朋友可以參考下
    2019-09-09
  • python解析xml簡單示例

    python解析xml簡單示例

    這篇文章主要介紹了python解析xml,結(jié)合簡單實例形式分析了Python針對城市信息xml文件的讀取、解析相關(guān)操作技巧,需要的朋友可以參考下
    2019-06-06

最新評論

陆河县| 巩义市| 黔东| 灵寿县| 普陀区| 海阳市| 蒙阴县| 郓城县| 西丰县| 阿拉善右旗| 吉水县| 遂川县| 宁城县| 德阳市| 和硕县| 永平县| 上思县| 侯马市| 芮城县| 曲沃县| 邵阳县| 苗栗县| 綦江县| 河津市| 三台县| 大港区| 区。| 阳朔县| 侯马市| 揭西县| 霸州市| 聂荣县| 滕州市| 两当县| 阿拉善右旗| 栾城县| 平谷区| 新源县| 五家渠市| 东至县| 瑞金市|