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。

更新于:2022年10月31日

4K+ 浏览量

启动您的职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.