You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何判断DataFrame是否存在重复行并返回指定提示?

Check for Duplicate Rows in DataFrame and Return Custom Message

Hey there! Here's a straightforward way to check if your DataFrame has any duplicate rows and output the exact message you need when duplicates exist:

Step-by-Step Solution

  1. Use df.duplicated().any() to detect if there’s at least one duplicate row. The duplicated() method flags each row as True if it’s a duplicate, and any() returns True if any duplicates are present in the result.
  2. Wrap this check in a conditional statement to print your desired message when duplicates are found.

Full Code Example

import pandas as pd

# Recreate your sample DataFrame
data = {
    'Name': ['Jack', 'Riti', 'Aadi', 'Riti', 'Riti', 'Riti', 'Aadi', 'Sachin'],
    'Age': [34, 30, 16, 30, 30, 30, 40, 30],
    'City': ['Sydney', 'Delhi', 'New York', 'Delhi', 'Delhi', 'Mumbai', 'London', 'Delhi']
}
df = pd.DataFrame(data)

# Check for duplicates and print the target message
if df.duplicated().any():
    print('The df contains duplicate rows')

Quick Notes

  • By default, duplicated() marks a row as duplicate if it matches a previously seen row (using keep='first'). If you want to check duplicates based on specific columns instead of the entire row, use the subset parameter—for example: df.duplicated(subset=['Name', 'Age']).any().
  • If you ever need to count the total number of duplicate rows, replace any() with sum(): df.duplicated().sum() will give you the exact count.

内容的提问来源于stack exchange,提问作者Krush23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 00:42:32