pandas庫中?DataFrame的用法小結(jié)
DataFrame 是一個表格型的數(shù)據(jù)結(jié)構(gòu),它含有一組有序的列,每列可以是不同的值類型(數(shù)值、字符串、布爾型值)。DataFrame 既有行索引也有列索引,它可以被看做由 Series 組成的字典(共同用一個索引)。
利用pandas.DataFrame可以構(gòu)建表格,通過列標屬性調(diào)用列對象
一、構(gòu)建表格
舉例
import pandas as pd x = [ ['PyTorch', '-', '.pt', True, True], ['TorchScript', 'torchscript', '.torchscript', True, True], ['ONNX', 'onnx', '.onnx', True, True], ['OpenVINO', 'openvino', '_openvino_model', True, False], ['TensorRT', 'engine', '.engine', False, True], ['CoreML', 'coreml', '.mlmodel', True, False], ['TensorFlow SavedModel', 'saved_model', '_saved_model', True, True], ['TensorFlow GraphDef', 'pb', '.pb', True, True], ['TensorFlow Lite', 'tflite', '.tflite', True, False], ['TensorFlow Edge TPU', 'edgetpu', '_edgetpu.tflite', False, False], ['TensorFlow.js', 'tfjs', '_web_model', False, False], ['PaddlePaddle', 'paddle', '_paddle_model', True, True],] df1 = pd.DataFrame(x, columns=['Format', 'Argument', 'Suffix', 'CPU', 'GPU']) df2 = pd.DataFrame(x, index=list(['a','b','c','d','e','f','g','q','w','e','r','t']),columns=['Format', 'Argument', 'Suffix', 'CPU', 'GPU']) print(df1) print('=======================================') print(df2)
輸出結(jié)果
Format Argument Suffix CPU GPU
0 PyTorch - .pt True True
1 TorchScript torchscript .torchscript True True
2 ONNX onnx .onnx True True
3 OpenVINO openvino _openvino_model True False
4 TensorRT engine .engine False True
5 CoreML coreml .mlmodel True False
6 TensorFlow SavedModel saved_model _saved_model True True
7 TensorFlow GraphDef pb .pb True True
8 TensorFlow Lite tflite .tflite True False
9 TensorFlow Edge TPU edgetpu _edgetpu.tflite False False
10 TensorFlow.js tfjs _web_model False False
11 PaddlePaddle paddle _paddle_model True True
=======================================
Format Argument Suffix CPU GPU
a PyTorch - .pt True True
b TorchScript torchscript .torchscript True True
c ONNX onnx .onnx True True
d OpenVINO openvino _openvino_model True False
e TensorRT engine .engine False True
f CoreML coreml .mlmodel True False
g TensorFlow SavedModel saved_model _saved_model True True
q TensorFlow GraphDef pb .pb True True
w TensorFlow Lite tflite .tflite True False
e TensorFlow Edge TPU edgetpu _edgetpu.tflite False False
r TensorFlow.js tfjs _web_model False False
t PaddlePaddle paddle _paddle_model True True
可以看出 index參數(shù)為行標設(shè)置,columns為列標設(shè)置,且都需為列表形式,長度都需要與給出的列表橫列數(shù)量一致(例子中的x)。
二、調(diào)用列對象和其中的屬性
import pandas as pd x = [ ['PyTorch', '-', '.pt', True, True], ['TorchScript', 'torchscript', '.torchscript', True, True], ['ONNX', 'onnx', '.onnx', True, True], ['OpenVINO', 'openvino', '_openvino_model', True, False], ['TensorRT', 'engine', '.engine', False, True], ['CoreML', 'coreml', '.mlmodel', True, False], ['TensorFlow SavedModel', 'saved_model', '_saved_model', True, True], ['TensorFlow GraphDef', 'pb', '.pb', True, True], ['TensorFlow Lite', 'tflite', '.tflite', True, False], ['TensorFlow Edge TPU', 'edgetpu', '_edgetpu.tflite', False, False], ['TensorFlow.js', 'tfjs', '_web_model', False, False], ['PaddlePaddle', 'paddle', '_paddle_model', True, True],] df1 = pd.DataFrame(x, columns=['Format', 'Argument', 'Suffix', 'CPU', 'GPU']) df2 = pd.DataFrame(x, index=list(['a','b','c','d','e','f','g','q','w','e','r','t']),columns=['Format', 'Argument', 'Suffix', 'CPU', 'GPU']) # print(df1) # print('=======================================') # print(df2) print(df1.Suffix) print('=====================================') print(df2.Format)
結(jié)合這一中的輸出表看,其輸出結(jié)果如下
0 .pt
1 .torchscript
2 .onnx
3 _openvino_model
4 .engine
5 .mlmodel
6 _saved_model
7 .pb
8 .tflite
9 _edgetpu.tflite
10 _web_model
11 _paddle_model
Name: Suffix, dtype: object
=====================================
a PyTorch
b TorchScript
c ONNX
d OpenVINO
e TensorRT
f CoreML
g TensorFlow SavedModel
q TensorFlow GraphDef
w TensorFlow Lite
e TensorFlow Edge TPU
r TensorFlow.js
t PaddlePaddle
Name: Format, dtype: object
可以看到 輸出的是一個 列的類實例,若繼續(xù)調(diào)用這個列中的每個元素,可以通過下列語句實現(xiàn)
print(df1.Suffix[0]) print('=====================================') print(df2.Format[1]) print('=====================================')
即通過索引調(diào)用,輸出為
.pt
=====================================
TorchScript
=====================================
或者通過該屬性所在的行標進行調(diào)用
print(df2.Format['a'])
輸出為
PyTorch
三、其中的屬性debug
四、怎么獲得行
目前還不清楚,上面的debug顯示其不包含具有 行信息的屬性,不過可以通過 values這個屬性來調(diào)用行,
values也是個類實例,其值為numpy矩陣,所以通過矩陣形式調(diào)用行,例如
print(df1.values[0, :]) >>['PyTorch' '-' '.pt' True True]
到此這篇關(guān)于pandas庫中 DataFrame的用法的文章就介紹到這了,更多相關(guān)pandas庫DataFrame用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
django xadmin action兼容自定義model權(quán)限教程
這篇文章主要介紹了django xadmin action兼容自定義model權(quán)限教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03面向新手解析python Beautiful Soup基本用法
這篇文章主要介紹了面向新手解析python Beautiful Soup基本用法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07Python使用pptx實現(xiàn)復(fù)制頁面到其他PPT中
這篇文章主要為大家詳細介紹了python如何使用pptx庫實現(xiàn)從一個ppt復(fù)制頁面到另一個ppt里面,文中的示例代碼講解詳細,感興趣的可以嘗試一下2023-02-02python中的[1:]、[::-1]、X[:,m:n]和X[1,:]的使用
本文主要介紹了python中的[1:]、[::-1]、X[:,m:n]和X[1,:]的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08