Pandas: Unexpected behavior when using DataFrame.drop_duplicates() on a subset of columns
I've looked through the documentation and I'm still confused about I'm encountering strange behavior when trying to remove duplicate rows from a DataFrame based on a subset of columns. I have a DataFrame with several columns, and I want to keep only the unique combinations of values from two specific columns while retaining the first occurrence of each unique combination. However, after applying `drop_duplicates()`, I noticed that the resulting DataFrame sometimes retains rows that I expected to be dropped. Here's a simplified version of what I'm working with: ```python import pandas as pd data = { 'A': [1, 1, 2, 2, 3], 'B': ['x', 'x', 'y', 'z', 'x'], 'C': [10, 10, 20, 30, 40] } df = pd.DataFrame(data) # I want to drop duplicates based on columns 'A' and 'B' result = df.drop_duplicates(subset=['A', 'B'], keep='first') print(result) ``` On running this, I expect the output to have only unique combinations of 'A' and 'B', but I am getting: ``` A B C 0 1 x 10 2 2 y 20 3 2 z 30 4 3 x 40 ``` I expected the second occurrence of (1, 'x') to be dropped entirely. Instead, it seems to be retained because the value in column 'C' is also considered. I've tried using `keep='last'` and `keep=False`, but that still doesn't give me the results I expect. What am I missing here? Is there a way to ensure that the duplicates are dropped correctly based only on the selected subset of columns? I've also confirmed I am using pandas version 1.5.3. Any insights or suggestions to resolve this would be greatly appreciated! My development environment is macOS. Any ideas what could be causing this?