目錄
Python 中的資料框
Dataframe的應用程式
建立範例資料框
#演算法(步驟)
範例
輸出
Python 中的矩陣
矩陣的應用
透過將矩陣轉換為資料幀進行矩陣乘法
矩陣與資料框
結論
首頁 後端開發 Python教學 在Python Pandas中,資料幀(data frames)和矩陣(matrices)之間的差異是什麼?

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

Sep 14, 2023 pm 07:53 PM

在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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何使用Python查找文本文件的ZIPF分佈 如何使用Python查找文本文件的ZIPF分佈 Mar 05, 2025 am 09:58 AM

如何使用Python查找文本文件的ZIPF分佈

如何在Python中下載文件 如何在Python中下載文件 Mar 01, 2025 am 10:03 AM

如何在Python中下載文件

python中的圖像過濾 python中的圖像過濾 Mar 03, 2025 am 09:44 AM

python中的圖像過濾

我如何使用美麗的湯來解析HTML? 我如何使用美麗的湯來解析HTML? Mar 10, 2025 pm 06:54 PM

我如何使用美麗的湯來解析HTML?

如何使用Python使用PDF文檔 如何使用Python使用PDF文檔 Mar 02, 2025 am 09:54 AM

如何使用Python使用PDF文檔

如何在django應用程序中使用redis緩存 如何在django應用程序中使用redis緩存 Mar 02, 2025 am 10:10 AM

如何在django應用程序中使用redis緩存

引入自然語言工具包(NLTK) 引入自然語言工具包(NLTK) Mar 01, 2025 am 10:05 AM

引入自然語言工具包(NLTK)

如何使用TensorFlow或Pytorch進行深度學習? 如何使用TensorFlow或Pytorch進行深度學習? Mar 10, 2025 pm 06:52 PM

如何使用TensorFlow或Pytorch進行深度學習?

See all articles