NumPy實(shí)現(xiàn)結(jié)構(gòu)化數(shù)組的示例代碼
在 NumPy 中,結(jié)構(gòu)化數(shù)組允許我們創(chuàng)建具有復(fù)雜數(shù)據(jù)類型的數(shù)組,類似于表格或數(shù)據(jù)庫(kù)中的行。這對(duì)于處理異質(zhì)數(shù)據(jù)集非常有用。在本篇博客中,我們將深入介紹 NumPy 中的結(jié)構(gòu)化數(shù)組,并通過(guò)實(shí)例演示如何創(chuàng)建、訪問(wèn)和操作結(jié)構(gòu)化數(shù)組。
1. 創(chuàng)建結(jié)構(gòu)化數(shù)組
結(jié)構(gòu)化數(shù)組可以通過(guò)指定每個(gè)字段的名稱和數(shù)據(jù)類型來(lái)創(chuàng)建。
import numpy as np # 定義數(shù)據(jù)類型 dtype = np.dtype([('name', 'S10'), ('age', int), ('height', float)]) # 創(chuàng)建結(jié)構(gòu)化數(shù)組 data = np.array([('Alice', 25, 5.6), ('Bob', 30, 6.0)], dtype=dtype) print(data)
2. 訪問(wèn)結(jié)構(gòu)化數(shù)組的字段
可以通過(guò)字段名稱訪問(wèn)結(jié)構(gòu)化數(shù)組的各個(gè)字段。
# 訪問(wèn)結(jié)構(gòu)化數(shù)組的字段 print(data['name']) # 輸出:['Alice' 'Bob'] print(data['age']) # 輸出:[25 30] print(data['height']) # 輸出:[5.6 6. ]
3. 修改結(jié)構(gòu)化數(shù)組的值
通過(guò)索引和字段名稱,可以修改結(jié)構(gòu)化數(shù)組的各個(gè)字段的值。
# 修改結(jié)構(gòu)化數(shù)組的值 data['age'][0] = 26 data['height'][1] = 6.2 print(data)
4. 多維結(jié)構(gòu)化數(shù)組
結(jié)構(gòu)化數(shù)組可以是多維的,每個(gè)維度可以有不同的數(shù)據(jù)類型。
# 多維結(jié)構(gòu)化數(shù)組 dtype_multi = np.dtype([('matrix', [('row', int), ('column', int)]), ('value', float)]) data_multi = np.array([((1, 2), 3.5), ((3, 4), 1.2)], dtype=dtype_multi) print(data_multi)
5. 使用嵌套字段
結(jié)構(gòu)化數(shù)組支持嵌套字段,可以方便地處理嵌套結(jié)構(gòu)。
# 使用嵌套字段 dtype_nested = np.dtype([('info', [('name', 'S10'), ('age', int)]), ('height', float)]) data_nested = np.array([(('Alice', 25), 5.6), (('Bob', 30), 6.0)], dtype=dtype_nested) print(data_nested)
6. 結(jié)構(gòu)化數(shù)組的排序
可以使用 np.sort 函數(shù)對(duì)結(jié)構(gòu)化數(shù)組進(jìn)行排序。
# 結(jié)構(gòu)化數(shù)組的排序 sorted_data = np.sort(data, order='age') print(sorted_data)
7. 結(jié)構(gòu)化數(shù)組的條件篩選
可以使用條件來(lái)篩選結(jié)構(gòu)化數(shù)組中的數(shù)據(jù)。
# 結(jié)構(gòu)化數(shù)組的條件篩選 filtered_data = data[data['age'] > 26] print(filtered_data)
8. 結(jié)構(gòu)化數(shù)組與 Pandas DataFrame 的轉(zhuǎn)換
結(jié)構(gòu)化數(shù)組可以方便地與 Pandas DataFrame 進(jìn)行轉(zhuǎn)換。
import pandas as pd # 結(jié)構(gòu)化數(shù)組轉(zhuǎn)為 DataFrame df = pd.DataFrame(data) print(df)
9. 總結(jié)
結(jié)構(gòu)化數(shù)組是 NumPy 中用于處理異質(zhì)數(shù)據(jù)的重要工具,通過(guò)定義復(fù)雜的數(shù)據(jù)類型,我們可以創(chuàng)建具有不同字段的數(shù)組,類似于表格或數(shù)據(jù)庫(kù)中的行。結(jié)構(gòu)化數(shù)組提供了訪問(wèn)、修改、排序和條件篩選數(shù)據(jù)的靈活性,同時(shí)也方便與 Pandas DataFrame 進(jìn)行交互。希望本篇博客能夠幫助你更好地理解和運(yùn)用 NumPy 中的結(jié)構(gòu)化數(shù)組功能。
到此這篇關(guān)于NumPy實(shí)現(xiàn)結(jié)構(gòu)化數(shù)組的示例代碼的文章就介紹到這了,更多相關(guān)NumPy 結(jié)構(gòu)化數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
78行Python代碼實(shí)現(xiàn)現(xiàn)微信撤回消息功能
這篇文章主要介紹了78行Python代碼實(shí)現(xiàn)現(xiàn)微信撤回消息功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07pytorch之深度神經(jīng)網(wǎng)絡(luò)概念全面整理
這篇文章主要介紹了pytorch之深度神經(jīng)網(wǎng)絡(luò)概念,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09在CentOS上配置Nginx+Gunicorn+Python+Flask環(huán)境的教程
這篇文章主要介紹了在CentOS上配置Nginx+Gunicorn+Python+Flask環(huán)境的教程,包括安裝supervisor來(lái)管理進(jìn)程的用法,整套配下來(lái)相當(dāng)實(shí)用,需要的朋友可以參考下2016-06-06Python中for循環(huán)可迭代對(duì)象迭代器及生成器源碼學(xué)習(xí)
這篇文章主要為大家介紹了Python中for循環(huán)可迭代對(duì)象迭代器及生成器的源碼學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05pycharm debug 斷點(diǎn)調(diào)試心得分享
這篇文章主要介紹了pycharm debug 斷點(diǎn)調(diào)試心得分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04Keras官方中文文檔:性能評(píng)估Metrices詳解
這篇文章主要介紹了Keras官方中文文檔:性能評(píng)估Metrices詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06