> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Compare two Rows in Pandas
- URL: https://datascientyst.com/compare-two-rows-pandas/
- Published: 2022-08-05T13:56:22.000Z
- Updated: 2022-08-05T13:56:22.000Z
- Author: John D K
- Tags: Row

In this short guide, we'll see how to **compare rows in Pandas DataFrame.** We will also cover how to find the difference between two rows in Pandas.

To compare columns or DataFrames in Pandas please refer to:

- [How to Compare Two Pandas DataFrames and Get Differences](https://datascientyst.com/compare-two-pandas-dataframes-get-differences/)
- <https://datascientyst.com/compare-two-columns-highlight-pandas/>

## 2\. Setup

In the post, we'll use the following DataFrame, which consists of several rows and columns:

```python
import pandas as pd
data = [('A',1, 0, 3, 1),
        ('A',1, 2, 5, 1),
        ('B',2, 1, 4, 3),
       ('B',3, 1, 0, 3),
       ('C',4, 3, 1, 2)]
cols = ('col_1', 'col_2', 'col_3', 'col_4', 'col_5' )
df = pd.DataFrame(data,
                 columns = cols)

```

Data is:

|   | col\_1 | col\_2 | col\_3 | col\_4 | col\_5 |
| - | ------ | ------ | ------ | ------ | ------ |
| 0 | A      | 1      | 0      | 3      | 1      |
| 1 | A      | 1      | 2      | 5      | 1      |
| 2 | B      | 2      | 1      | 4      | 3      |
| 3 | B      | 3      | 1      | 0      | 3      |
| 4 | C      | 4      | 3      | 1      | 2      |

## Step 1: Compare two rows

Pandas offers the method `compare()` which can be used in order of two rows in Pandas.

Let's check how we can use it to compare specific rows in DataFrame. We are going to compare row with index - 0 to row - 2:

```python
df.loc[0].compare(df.loc[2])

```

The result is all values which has difference:

|        | self | other |
| ------ | ---- | ----- |
| col\_1 | A    | B     |
| col\_2 | 1    | 2     |
| col\_3 | 0    | 1     |
| col\_4 | 3    | 4     |
| col\_5 | 1    | 3     |

Comparing the first two rows will give us only the differences:

|        | self | other |
| ------ | ---- | ----- |
| col\_3 | 0    | 2     |
| col\_4 | 3    | 5     |

![](https://datascientyst.com/content/images/2022/08/compare-two-rows-pandas.png)

## Step 2: Compare two rows with highlighting

To compare 2 rows while showing highlighted differences can be achieved with a custom function.

First we will select the rows for comparison and transpose the resulting DataFrame by `df.loc[:1].T`. Then we will apply the function for comparison:

```python
def highlight(d):
    df = pd.DataFrame(columns=d.columns, index=d.index)
    
    col1 = d.columns[0]
    col2 = d.columns[1]
    df[[col1, col2]] = 'background: None'
    
    df.loc[d[col1].ne(d[col2]), [col1, col2]] = 'background: yellow'

    return df
    
df.loc[:1].T.style.apply(highlight, axis=None)

```

the result is:

|        | 0 | 1 |
| ------ | - | - |
| col\_1 | A | A |
| col\_2 | 1 | 1 |
| col\_3 | 0 | 2 |
| col\_4 | 3 | 5 |
| col\_5 | 1 | 1 |

## Step 3: Calculate the difference between rows

To calculate the difference between rows - row by row we can use the function - `diff()`. To calculated difference for `col_4`:

```python
df['col_4'].diff()

```

the result would be difference calculated for each two rows:

```
0    NaN
1    2.0
2   -1.0
3   -4.0
4    1.0
Name: col_4, dtype: float64

```

Method `diff()` has two useful parameters:

- `periods` \- shift of periods for calculating difference
- `axis` \- rows or columns

If there are string values in some rows you may face the following error:

> TypeError: unsupported operand type(s) for -: 'str' and 'str'

## Step 4: Compare rows from different DataFrames

In order to compare rows from different DataFrames we can select them by index:

```python
df2 = df.copy()
df.loc[0].compare(df2.loc[1])

```

Another option is to match rows on another column - let say unique column - `url`. We can match the records by:

```python
url_same = df1[df1.url.isin(df2.url)].url

```

Then we can select one by:

```python
url = url_same[2]

```

Finally we can concatenated into single DataFrame as:

```python
pd.concat([df1[df1.url == url], df2[df2.url == url]]).T

```

Finally we can transpose the result for readability. The full code:

```python
url_same = df1[df1.url.isin(df2.url)].url
url = url_same[2]

df_c = pd.concat([df1[df1.url == url], df2[df2.url == url]]).T

```

## Step 5: Finding the duplicate rows

We can find the duplicated rows in the whole DataFrame by command like:

```python
df[['col_1', 'col_2']].duplicated()

```

Method `duplicated()` returns boolean array for duplicate rows:

```
0    False
1     True
2    False
3    False
4    False
dtype: bool

```

or for the whole DataFrame. In this case we will get also the data:

```python
df[df.duplicated()]

```

Empty DataFrame since there no duplicated rows on every column

## Step 6: Finding the unique rows

To find all the unique rows we can use:

```python
df.drop_duplicates()

```

or for subset of columns:

```python
df[['col_1', 'col_2']].drop_duplicates()

```

result:

|   | col\_1 | col\_2 |
| - | ------ | ------ |
| 0 | A      | 1      |
| 2 | B      | 2      |
| 3 | B      | 3      |
| 4 | C      | 4      |

## Conclusion

In this article we show how to compare rows, compare rows with highlighting and extract the difference of two rows. We covered comparison of rows from different DataFrames and calculating the difference between rows for the whole DataFrame.