gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
dataframe drop duplicate row was on columns
- User
dataframe drop duplicate row was on columns
- Assistant
If you want to drop duplicate rows in a pandas DataFrame based on specific columns, you can use the
drop_duplicates()method and specify the subset of columns to consider for identifying duplicates.Here's an example:
import pandas as pd # Sample dataframe data = { 'A': [1, 2, 2, 3, 4], 'B': [5, 6, 6, 7, 8], 'C': ['x', 'y', 'y', 'z', 'w'] } df = pd.DataFrame(data) print("Original DataFrame:") print(df) # Drop duplicate rows based on columns 'A' and 'B' df_no_duplicates = df.drop_duplicates(subset=['A', 'B']) print("\nDataFrame after dropping duplicates on columns 'A' and 'B':") print(df_no_duplicates)Explanation:
subset=['A', 'B']: Check duplicates only based on columns 'A' and 'B'.- Rows with duplicate values in both columns 'A' and 'B' will be dropped, keeping the first occurrence by default.
You can also specify
keep='last'to keep the last occurrence orkeep=Falseto drop all duplicates.If you want me to help with a specific dataframe or columns, just let me know!