Pandas DataFrame에서 중복 값을 제거하려면 drop_duplicates() 메서드를 사용하세요. 먼저 3개의 열이 있는 DataFrame을 만듭니다. -
dataFrame = pd.DataFrame({'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'],'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'],'UnitsSold': [95, 70, 80, 95, 70, 90]})
중복 값 제거 -
dataFrame = dataFrame.drop_duplicates()
예시
다음은 전체 코드입니다 -
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'],'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], 'UnitsSold': [95, 70, 80, 95, 70, 90]}) print"Dataframe...\n", dataFrame # counting frequency of column Car count = dataFrame['Car'].value_counts() print"\nCount in column Car" print(count) # removing duplicates dataFrame = dataFrame.drop_duplicates() print"\nUpdated DataFrame after removing duplicates...\n",dataFrame # counting frequency of column Car after removing duplicates count = dataFrame['Car'].value_counts() print"\nCount in column Car" print(count)
출력
이것은 다음과 같은 출력을 생성합니다 -
Dataframe... Car Place UnitsSold 0 BMW Delhi 95 1 Mercedes Hyderabad 70 2 Lamborghini Chandigarh 80 3 BMW Delhi 95 4 Mercedes Hyderabad 70 5 Porsche Mumbai 90 Count in column Car BMW 2 Mercedes 2 Porsche 1 Lamborghini 1 Name: Car, dtype: int64 Updated DataFrame after removing duplicates... Car Place UnitsSold 0 BMW Delhi 95 1 Mercedes Hyderabad 70 2 Lamborghini Chandigarh 80 5 Porsche Mumbai 90 Count in column Car BMW 1 Porsche 1 Lamborghini 1 Mercedes 1 Name: Car, dtype: int64