Pandas DataFrame の eq メソッド

Beginner

はじめに

この実験では、Python の pandas ライブラリのeq()メソッドを使って DataFrame 内の値を比較する方法を学びます。eq()メソッドは DataFrame 内の等しい値をチェックし、要素が等しいかどうかを示すブール値の新しい DataFrame を返します。

VM のヒント

VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。

時々、Jupyter Notebook が読み込み終わるまで数秒待つ必要がある場合があります。Jupyter Notebook の制限により、操作の検証は自動化できません。

学習中に問題がある場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。

pandas ライブラリをインポートする

まず、DataFrame を扱うために必要な pandas ライブラリをインポートします。

import pandas as pd

DataFrame を作成する

次に、比較に使用する DataFrame を作成しましょう。'Roll no'と'Marks'の 2 つの列と 4 つの行からなる 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 の新しい列に追加する方法も学びました。