这篇文章主要介绍了pandas库中 DataFrame的用法,利用pandas.DataFrame可以构建表格,通过列标属性调用列对象,本文结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
DataFrame 是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型值)。DataFrame 既有行索引也有列索引,它可以被看做由 Series 组成的字典(共同用一个索引)。
利用pandas.DataFrame可以构建表格,通过列标属性调用列对象
一、构建表格
举例
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)
输出结果
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参数为行标设置,columns为列标设置,且都需为列表形式,长度都需要与给出的列表横列数量一致(例子中的x)。
二、调用列对象和其中的属性
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)
结合这一中的输出表看,其输出结果如下
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
可以看到 输出的是一个 列的类实例,若继续调用这个列中的每个元素,可以通过下列语句实现
print(df1.Suffix[0]) print('=====================================') print(df2.Format[1]) print('=====================================')
即通过索引调用,输出为
.pt ===================================== TorchScript =====================================
或者通过该属性所在的行标进行调用
print(df2.Format['a'])
输出为
PyTorch
三、其中的属性debug
四、怎么获得行
目前还不清楚,上面的debug显示其不包含具有 行信息的属性,不过可以通过 values这个属性来调用行,
values也是个类实例,其值为numpy矩阵,所以通过矩阵形式调用行,例如
print(df1.values[0, :]) >>['PyTorch' '-' '.pt' True True]
到此这篇关于pandas库中 DataFrame的用法的文章就介绍到这了
发表评论 取消回复