How do I find and drop duplicate rows in pandas?

Mark rows whose ticker and date appear more than once with duplicated(subset=…, keep=…), look at conflicting copies, and keep one row per ticker and date with drop_duplicates.

After this lesson, you can find conflicting closes in a price file and keep one close per ticker and date.

How do I mark repeated rows?

.duplicated() marks a row True when it repeats an earlier row, and subset=["ticker", "date"] compares only those columns, so it also finds two different closes for the same stock and day. In six made-up vendor records, row 4 repeats row 3 exactly, and row 2 gives AAA a second close on 3 March, 102.4 against 102.0.

The records are in arrival order, with closes in kronor. keep="first", the default, leaves the first copy unmarked, keep="last" the last one, and keep=False marks every copy, which shows both sides of a conflict.

The following box marks the rows three ways.

import pandas as pd

prices = pd.DataFrame({
    "ticker": ["AAA", "AAA", "AAA", "BBB", "BBB", "BBB"],
    "date": pd.to_datetime(["2026-03-02", "2026-03-03", "2026-03-03", "2026-03-02", "2026-03-02", "2026-03-03"]),
    "close": [101.2, 102.0, 102.4, 55.1, 55.1, 55.6],
})
pair = ["ticker", "date"]
prices["exact"] = prices.duplicated(subset=["ticker", "date", "close"])
prices["pair"] = prices.duplicated(subset=pair)
prices["all_copies"] = prices.duplicated(subset=pair, keep=False)
print(prices)
  ticker       date  close  exact   pair  all_copies
0    AAA 2026-03-02  101.2  False  False       False
1    AAA 2026-03-03  102.0  False  False        True
2    AAA 2026-03-03  102.4  False   True        True
3    BBB 2026-03-02   55.1  False  False        True
4    BBB 2026-03-02   55.1   True   True        True
5    BBB 2026-03-03   55.6  False  False       False

The pair that identifies a record depends on the file, and a file with one close per trading venue needs the venue in the pair too.

How do I keep one row per ticker and date?

.drop_duplicates(subset=pair, keep="last") keeps one row per ticker and date, the last to arrive, which suits a vendor whose later records correct earlier ones. AAA keeps 102.4 on 3 March, and checking .duplicated(subset=pair).any() on the result gives False.

ignore_index=True numbers the remaining rows from 0. A conflict without a rule for the right copy belongs in a review, and a backtest uses the record available when the trading decision was made.

The following box keeps the last copy of each pair and checks the result.

clean = prices.drop_duplicates(subset=pair, keep="last", ignore_index=True)[["ticker", "date", "close"]]
print(clean)
print(clean.duplicated(subset=pair).any())
  ticker       date  close
0    AAA 2026-03-02  101.2
1    AAA 2026-03-03  102.4
2    BBB 2026-03-02   55.1
3    BBB 2026-03-03   55.6
False

Your turn

Count the ticker and date pairs that appear more than once. You should see 2.

The marked copies, reduced to one row per pair, are the repeated pairs.

print(len(prices.loc[prices["all_copies"], pair].drop_duplicates()))
2