Python Pandas 中的数据框和矩阵有什么区别?
在本文中,我们将向您展示 Python Pandas 中数据框和矩阵的区别。
数据框和矩阵都是二维数据结构。一般来说,数据框可以包含多种类型的数据(数字、字符、因子等),而矩阵只能存储一种类型的数据。
Python 中的数据框
在 Python 中,DataFrame 是一种二维、表格、可变的数据结构,可以存储包含各种数据类型对象的表格数据。DataFrame 具有以行和列形式标记的轴。DataFrame 是数据预处理中的有用工具,因为它们提供了有价值的数据处理方法。 DataFrames 还可用于创建数据透视表并使用 Matplotlib 绘制数据。
Dataframe 的应用
数据框可以执行各种任务,例如拟合统计公式。
数据处理(矩阵无法实现,必须先转换为数据框)
将行转置为列,反之亦然,这在数据科学中很有用。
创建示例数据框
算法(步骤)
以下是执行所需任务所要遵循的算法/步骤 −
使用 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 支持矩阵上的多种算术运算,例如加法、减法、乘法和除法。
矩阵的应用
它在经济学中非常有用,可用于计算 GDP(国内生产总值)或 PI(人均收入)等统计数据。
它对于研究电气和电子电路也很有用。
打印输入数据框。
矩阵用于调查研究,例如绘制图表。
这在概率和统计中很有用。
通过将矩阵转换为数据框进行矩阵乘法
算法(步骤)
以下是执行所需任务要遵循的算法/步骤−
使用 import 关键字,以别名导入 pandas 模块。
创建两个变量分别存储两个输入矩阵。
使用 pandas 模块的 DataFrame() 函数(创建数据框)创建第一和第二个矩阵的数据框,并将它们存储在单独的变量中。这里将数据加载到 pandas DataFrames 中。
打印输入矩阵 1 的数据框。
通过将 shape 属性应用于输入矩阵 1,打印其尺寸(形状)。
打印输入矩阵 2 的数据框。
通过将 shape 属性应用于输入矩阵 2,打印其尺寸(形状)。
使用 dot() 函数将矩阵 inputMatrix_1 和 inputMatrix_2 相乘,并创建一个变量来存储它。
打印 inputMatrix_1 和 inputMatrix_2 矩阵乘法的结果矩阵。
通过将 shape 属性应用于输入矩阵 2,打印结果矩阵的尺寸(形状)。它。
示例
以下程序使用 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 数组 | 数据框是长度相同的向量列表。数据框是矩阵的广义形式。 |
矩阵的行数和列数是固定的。 | 数据框的行数和列数是可变的。 |
同质 | 异质 |
结论
我们在本程序中了解了 Python 中矩阵和数据框之间的区别。我们还学习了如何制作数据框以及如何将矩阵转换为数据框。