Pandas DataFrame eq 메서드

Beginner

소개

이 랩에서는 Python pandas 라이브러리의 eq() 메서드를 사용하여 DataFrame 의 값을 비교하는 방법을 배웁니다. eq() 메서드는 DataFrame 에서 동일한 값을 확인하고, 요소가 같은지 여부를 나타내는 부울 값의 새로운 DataFrame 을 반환합니다.

VM 팁

VM 시작이 완료되면, 왼쪽 상단 모서리를 클릭하여 Notebook 탭으로 전환하여 실습을 위해 Jupyter Notebook에 접근하십시오.

때로는 Jupyter Notebook 이 로딩을 완료하는 데 몇 초 정도 기다려야 할 수 있습니다. Jupyter Notebook 의 제한으로 인해 작업의 유효성 검사는 자동화될 수 없습니다.

학습 중에 문제가 발생하면 Labby 에게 문의하십시오. 세션 후 피드백을 제공해주시면 문제를 즉시 해결해 드리겠습니다.

pandas 라이브러리 가져오기

먼저, DataFrame 작업을 가능하게 해주는 pandas 라이브러리를 임포트해야 합니다.

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)

Output:

-------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))

Output:

----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]))

Output:

----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))

Output:

----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]))

Output:

-------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)

Output:

-------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 의 새 열에 추가하는 방법도 배웠습니다.