Python Pandas中DataFrame和矩陣的區別?
在本文中,我們將向您展示Python Pandas中DataFrame和矩陣之間的區別。
DataFrame和矩陣都是二維資料結構。一般來說,DataFrame可以包含多種型別的資料(數值型、字元型、因子型等),而矩陣只能儲存一種型別的資料。
Python中的DataFrame
在Python中,DataFrame是一個二維的、表格狀的、可變的資料結構,可以儲存包含各種資料型別物件的表格資料。DataFrame具有以行和列形式標記的軸。DataFrame在資料預處理中是很有用的工具,因為它提供了寶貴的資料處理方法。DataFrame還可以用於建立透視表和使用Matplotlib繪製資料。
DataFrame的應用
DataFrame可以執行各種任務,例如擬合統計公式。
資料處理(矩陣無法進行,必須先轉換為DataFrame)
可以將行轉置為列,反之亦然,這在資料科學中非常有用。
建立示例DataFrame
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用import關鍵字匯入**pandas、numpy**模組,並使用別名。
使用pandas模組的**DataFrame()**函式建立一個DataFrame。
列印輸入DataFrame。
示例
下面的程式使用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支援矩陣的多種算術運算,例如加法、減法、乘法和除法。
矩陣的應用
它在經濟學中非常有用,可以計算GDP(國內生產總值)或人均收入等統計資料。
它也適用於研究電氣和電子電路。
列印輸入DataFrame。
矩陣用於調查研究,例如繪製圖表。
這在機率和統計中很有用。
透過將矩陣轉換為DataFrame進行矩陣乘法
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用import關鍵字匯入**pandas**模組,並使用別名。
建立兩個變數分別儲存兩個輸入矩陣。
使用pandas模組的**DataFrame()**函式(建立DataFrame)為第一個和第二個矩陣建立DataFrame,並將它們儲存在單獨的變數中。此處資料載入到pandas DataFrame中。
列印輸入矩陣1的DataFrame。
透過應用**shape**屬性來列印輸入矩陣1的維度(形狀)。
列印輸入矩陣2的DataFrame。
透過應用**shape**屬性來列印輸入矩陣2的維度(形狀)。
使用**dot()**函式將兩個矩陣inputMatrix_1和inputMatrix_2相乘,並建立一個變數來儲存結果。
列印inputMatrix_1和inputMatrix_2矩陣乘法的結果矩陣。
透過應用shape屬性來列印結果矩陣的維度(形狀)。
示例
下面的程式使用DataFrame()函式返回一個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)
以下是矩陣和DataFrame的區別表。
矩陣 vs DataFrame
矩陣 | DataFrame |
---|---|
它是組織在二維矩形結構中的資料集的集合 | 它儲存具有多個數據型別的資料表,這些資料表位於多個稱為欄位的列中。 |
矩陣是一個m*n陣列,具有相同的資料型別 | DataFrame是長度相同的向量的列表。DataFrame是矩陣的泛化形式。 |
矩陣具有固定數量的行和列。 | DataFrame具有可變數量的行和列。 |
同質的 | 異質的 |
結論
在這個程式中,我們學習了Python中矩陣和DataFrame的區別。我們還學習瞭如何建立DataFrame以及如何將矩陣轉換為DataFrame。