Pandas DataFrame Eq 方法

PythonPythonBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将学习如何在 Python 的 pandas 库中使用 eq() 方法来比较 DataFrame 中的值。eq() 方法用于检查 DataFrame 中的值是否相等,并返回一个新的布尔值 DataFrame,指示元素是否相等。

虚拟机提示

虚拟机启动完成后,点击左上角切换到 Notebook 标签页,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟,直到 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时可以向 Labby 提问。实验结束后请提供反馈,我们将及时为你解决问题。

导入 pandas 库

首先,我们需要导入 pandas 库,这将使我们能够操作 DataFrame。

import pandas as pd

创建 DataFrame

接下来,让我们创建一个用于比较的 DataFrame。我们将创建一个包含两列('Roll no' 和 'Marks')和四行的 DataFrame。

df = pd.DataFrame({"Roll no": [100, 101, 102, 103], "Marks": [60, 62, 65, 59]}, index=["Saanvi", "Hasini", "Lakshmi", "Kushi"])
print("-------The DataFrame is---------")
print(df)

输出:

-------The DataFrame is---------
         Roll no  Marks
Saanvi       100     60
Hasini       101     62
Lakshmi      102     65
Kushi        103     59

比较 DataFrame 元素与指定值

现在,让我们使用 eq() 方法将 DataFrame 的元素与指定值进行比较。我们将 DataFrame 的元素与值 62 进行比较。

print("----Find the comparison of the dataframe element with value----")
print(df.eq(62))

输出:

----Find the comparison of the dataframe element with value----
         Roll no  Marks
Saanvi     False  False
Hasini     False   True
Lakshmi    False  False
Kushi      False  False

比较不同列与不同值

我们还可以将 DataFrame 的不同列与不同的值进行比较。让我们将 'Roll no' 列与值 101 进行比较,并将 'Marks' 列与值 62 进行比较。

print("----Find the comparison of the dataframe element----")
print(df.eq([101, 62]))

输出:

----Find the comparison of the dataframe element----
         Roll no  Marks
Saanvi     False  False
Hasini      True   True
Lakshmi    False  False
Kushi      False  False

比较指定列与值

我们还可以将 DataFrame 的指定列与特定值进行比较。让我们将 'Marks' 列与值 62 进行比较。

print("----Find the comparison of the dataframe element----")
print(df["Marks"].eq(62))

输出:

----Find the comparison of the dataframe element----
Saanvi     False
Hasini      True
Lakshmi    False
Kushi      False
Name: Marks, dtype: bool

比较指定列与不同值

我们可以将 DataFrame 的多个指定列与不同的值进行比较。让我们将 'Age' 和 'Weight' 列分别与值 20 和 60 进行比较。

chart = {'Name':['Chetan','Yashas','Yuvraj'], 'Age':[20, 25, 30], 'Height':[155, 170, 165],'Weight':[59, 60, 75]}
df = pd.DataFrame(chart)
print("-------The DataFrame is---------")
print(df)
print("----Find the comparison of the dataframe element----")
print(df[["Age", "Weight"]].eq([20, 60]))

输出:

-------The DataFrame is---------
     Name  Age  Height  Weight
0  Chetan   20     155      59
1  Yashas   25     170      60
2  Yuvraj   30     165      75
----Find the comparison of the dataframe element----
     Age  Weight
0   True   False
1  False    True
2  False   False

比较 DataFrame 列并将结果添加到新列

我们可以比较 DataFrame 的列,并将结果添加到新列中。让我们比较 'col1' 和 'col2' 列,并将结果添加到名为 'Result' 的新列中。

df = pd.DataFrame({"col1": [10, 30, 60, 40, 20], "col2": [10, 15, 60, 45, 20]})
print("-------The DataFrame is---------")
print(df)
print("----Find the comparison of the dataframe element----")
df['Result'] = df['col1'].eq(df['col2'])
print(df)

输出:

-------The DataFrame is---------
   col1  col2
0    10    10
1    30    15
2    60    60
3    40    45
4    20    20
----Find the comparison of the dataframe element----
   col1  col2  Result
0    10    10    True
1    30    15   False
2    60    60    True
3    40    45   False
4    20    20    True

总结

在本实验中,我们学习了如何使用 pandas 库中的 eq() 方法来比较 DataFrame 中的值。通过应用 eq() 方法,我们可以将元素与指定值进行比较,将不同列与不同值进行比较,将指定列与特定值进行比较,以及将选定列与不同值进行比较。我们还学习了如何将比较结果添加到 DataFrame 的新列中。