在Python Pandas中,資料幀(data frames)和矩陣(matrices)之間的差異是什麼?

WBOY
發布: 2023-09-14 19:53:02
轉載
1138 人瀏覽過

在Python Pandas中,数据帧(data frames)和矩阵(matrices)之间的区别是什么?

在本文中,我們將向您展示 python 中資料幀和矩陣之間的差異 貓熊.

資料框和矩陣都是二維資料結構。一般來說,資料幀可以包含多種類型的資料(數字、字元、因子等),而矩陣只能儲存一種類型的資料。

Python 中的資料框

在Python中,DataFrame是一種二維、表格、可變的資料結構,可以儲存包含各種資料類型的物件的表格資料。 DataFrame 具有以行和列形式標示的軸。 DataFrame 是資料預處理中有用的工具,因為它們提供了有價值的資料處理方法。 DataFrame 也可用於建立資料透視表並使用 Matplotlib 繪製資料。

Dataframe的應用程式

  • 資料框可以執行各種任務,例如擬合統計公式。

  • 資料處理(Matrix 不可能,必須先轉換為資料幀)

  • 將行轉換為列,反之亦然,這在資料科學中非常有用。

建立範例資料框

演算法(步驟)

以下是執行所需任務所需遵循的演算法/步驟 -

  • 使用 import 關鍵字,匯入帶有別名的 pandas、numpy 模組。

  • 使用 pandas 模組的 DataFrame() 函數建立資料框。

  • 列印輸入資料幀。

範例

以下程式使用 DataFrame() 函數傳回一個資料幀 -

# importing pandas, numpy modules with alias names
import pandas as pd
import numpy as np

# creating a dataframe
inputDataframe = pd.DataFrame({'Name': ['Virat', 'Rohit', 'Meera', 'Nick', 'Sana'], 'Jobrole': ['Developer', 'Analyst', 'Help Desk', 'Database Developer', 'Finance accountant'], 'Age': [25, 30, 28, 25, 40]})

# displaying the dataframe
print(inputDataframe)
登入後複製

輸出

執行時,上述程式將產生以下輸出 -

   Name             Jobrole      Age
0  Virat            Developer    25
1  Rohit            Analyst      30
2  Meera            Help Desk    28
3  Nick  Database   Developer    25
4  Sana  Finance    accountant   40
登入後複製

Python 中的矩陣

矩陣是以二維矩形網格組織的同構資料集集合。它是一個具有相同資料類型的 m*n 數組。它是用向量輸入創建的。有固定數量的行和列。 Python 支援 Matrix 上的加、減、乘、除等多種算術運算。

矩陣的應用

  • 它在經濟學中用於計算 GDP(國內生產毛額)或 PI(人均收入價格)等統計數據非常有用。

  • 它對於研究電氣和電子電路也很有用。

  • 列印輸入資料幀。

  • 矩陣用於調查研究,例如繪製圖表。

  • 這在機率和統計中很有用。

透過將矩陣轉換為資料幀進行矩陣乘法

#演算法(步驟)

以下是執行所需任務所需遵循的演算法/步驟 -

  • 使用 import 關鍵字匯入帶有別名的 pandas 模組。

  • 建立兩個變數來分別儲存兩個輸入矩陣。

  • 使用 pandas 模組的 DataFrame() 函數(建立資料幀)建立第一個和第二個矩陣的資料幀,並將它們儲存在單獨的變數中。這裡資料被載入到 pandas DataFrames 中。

  • 列印輸入矩陣1的資料幀。

  • 透過套用 shape 屬性來列印輸入矩陣 1 的尺寸(形狀)。

  • 列印輸入矩陣2的資料幀。

  • 透過套用 shape 屬性來列印輸入矩陣 2 的尺寸(形狀)。

  • 使用 dot() 函數將矩陣 inputMatrix_1 和 inputMatrix_2 相乘,並建立一個變數來儲存它。

  • 列印 inputMatrix_1 和 inputMatrix_2 矩陣相乘的結果矩陣。

  • 透過套用 shape 屬性來列印結果矩陣的尺寸(形狀)。

範例

以下程式使用 DataFrame() 函數傳回一個資料幀 -

# importing pandas module
import pandas as pd

# input matrix 1
inputMatrix_1 = [[1, 2, 2],
   [1,  2, 0],
   [1,  0, 2]]

# input matrix 2
inputMatrix_2 = [[1, 0, 1],
   [2, 1, 1],
   [2, 1, 2]]

# creating a dataframe of first matrix
#(here data is loaded into a pandas DataFrames)
df_1 = pd.DataFrame(data=inputMatrix_1)

# creating a dataframe of second matrix
df_2 = pd.DataFrame(data=inputMatrix_2)

# printing the dataframe of input matrix 1
print("inputMatrix_1:")
print(df_1)

# printing the dimensions(shape) of input matrix 1
print("The dimensions(shape) of input matrix 1:")
print(df_1.shape)
print()

# printing the dataframe of input matrix 2
print("inputMatrix_2:")
print(df_2)

# printing the dimensions(shape) of input matrix 1
print("The dimensions(shape) of input matrix 2:")
print(df_2.shape)
print()

# multiplying both the matrices inputMatrix_1 and inputMatrix_2
result_mult = df_1.dot(df_2)

# Printing the resultant of matrix multiplication of inputMatrix_1 and inputMatrix_2
print("Resultant Matrix after Matrix multiplication:")
print(result_mult)

# printing the dimensions(shape) of resultant Matrix
print("The dimensions(shape) of Resultant Matrix:")
print(result_mult.shape)
登入後複製

輸出

inputMatrix_1:
0 1 2
0 1 2 2
1 1 2 0
2 1 0 2
The dimensions(shape) of input matrix 1:
(3, 3)

inputMatrix_2:
0 1 2
0 1 0 1
1 2 1 1
2 2 1 2
The dimensions(shape) of input matrix 2:
(3, 3)

Resultant Matrix after Matrix multiplication:
0 1 2
0 9 4 7
1 5 2 3
2 5 2 5
The dimensions(shape) of Resultant Matrix:
(3, 3)
登入後複製

下面是矩陣和資料框的差異表。

矩陣與資料框

矩陣 資料框
它是以二維矩形組織排列的資料集的集合 它將具有多種資料類型的資料表儲存在稱為欄位的多個欄位中。
矩陣是一個m*n數組,資料型別相同 資料幀是相同長度的向量列表。資料框是矩陣的廣義形式。
矩陣具有固定數量的行和列。 Dataframe 的行數和列數是可變的。
同質 異構

結論

我們在這個程式中了解了Python中矩陣和資料框之間的差異。我們還學習如何製作資料框以及如何將矩陣轉換為資料框。

以上是在Python Pandas中,資料幀(data frames)和矩陣(matrices)之間的差異是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!