卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章20171本站已运行347

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

<?xml encoding="utf-8" ?>

在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将文本文件的奇数行复制到另一个文件中
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏