Pandas DataFrame | eq method
Start your free 7-days trial now!
Pandas DataFrame.eq(~) method returns a DataFrame of booleans where True indicates an entry that is equal to the specified value.
Missing values (NaN) are considered to be distinct, that is, NaN==NaN will evaluate to False.
Parameters
1. otherlink | scalar or sequence or Series or DataFrame
The value to check for equality.
2. axislink | string or int | optional
Whether to perform the comparison along the columns or the rows:
Axis | Description |
|---|---|
| Compare each column. |
| Compare each row. |
By default, axis=1.
3. level | int or string | optional
The levels to perform comparison on. This is only relevant if your source DataFrame is a multi-index.
Return Value
A DataFrame of booleans.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[1,2], "B":[3,4]})df
A B0 1 31 2 4
Element-wise comparisons
To check which values in df equal 3:
df.eq(3)
A B0 False True1 False False
Row-wise comparisons
To perform row-wise comparisons, pass an array-like structure like follows:
df.eq([1,2]) # axis=1
A B0 True False1 False False
Here, we are comparing each row of the source DataFrame with [1,2]. This means that we are performing the following pair-wise comparisons:
(row one) [1,3] == [1,2] = [True, False](row two) [2,4] == [1,2] = [False, False]
Column-wise comparisons
For your reference, we show the df here again:
df
A B0 1 31 2 4
To perform column-wise comparisons, pass an array-like structure and set axis=0:
df.eq([1,2], axis=0)
A B0 True False1 True False
Here, we are comparing each column of the source DataFrame with [1,2]. This means that we are performing the following pair-wise comparisons:
(column A) [1,2] == [1,2] = [True, True](column B) [3,4] == [1,2] = [False, False]