Python의 Pandas DataFrame 내에서 중복 항목을 식별하고 검색하는 방법은 무엇입니까?

Patricia Arquette
풀어 주다: 2024-10-25 11:31:02
원래의
826명이 탐색했습니다.

How to Identify and Retrieve Duplicate Items within a Pandas DataFrame in Python?

Python에서 Pandas를 사용하여 모든 중복 항목 목록을 가져오는 방법

데이터세트로 작업할 때 중복 항목을 접하는 것이 일반적입니다. . 이 경우 Pandas를 사용하여 데이터 세트의 모든 중복 항목을 식별하려고 합니다.

이를 달성하려면 다음 접근 방식을 활용할 수 있습니다.

방법 1(모든 행 인쇄 중복 ID):

<code class="python">import pandas as pd

# Read the CSV data into a DataFrame
df = pd.read_csv("dup.csv")

# Extract the "ID" column
ids = df["ID"]

# Create a new DataFrame with only the duplicate values
duplicates = df[ids.isin(ids[ids.duplicated()])]

# Sort the DataFrame by the "ID" column
duplicates.sort_values("ID", inplace=True)

# Print the duplicate values
print(duplicates)</code>
로그인 후 복사

방법 2(그룹화 및 중복 그룹 연결):

이 방법은 중복 그룹을 결합하여 간결하게 표현합니다. 중복 항목 수:

<code class="python"># Group the DataFrame by the "ID" column
grouped = df.groupby("ID")

# Filter the grouped DataFrame to include only groups with more than one row
duplicates = [g for _, g in grouped if len(g) > 1]

# Concatenate the duplicate groups into a new DataFrame
duplicates = pd.concat(duplicates)

# Print the duplicate values
print(duplicates)</code>
로그인 후 복사

방법 1 또는 방법 2를 사용하면 데이터 세트의 모든 중복 항목 목록을 성공적으로 얻을 수 있으므로 이를 시각적으로 검사하고 불일치를 조사할 수 있습니다.

위 내용은 Python의 Pandas DataFrame 내에서 중복 항목을 식별하고 검색하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!