12

I have a pandas dataframe as follows:

A   B   C
1   2   x
1   2   y
3   4   z
3   5   x

I want that only 1 row remains of rows that share the same values in specific columns. In the example above I mean columns A and B. In other words, if the values of columns A and B occur more than once in the dataframe, only one row should remain (which one does not matter).

FWIW: the maximum number of so called duplicate rows (that is, where column A and B are the same) is 2.

The result should looke like this:

A   B   C
1   2   x
3   4   z
3   5   x

or

A   B   C
1   2   y
3   4   z
3   5   x

1 Answer 1

26

Use drop_duplicates with parameter subset, for keeping only last duplicated rows add keep='last':

df1 = df.drop_duplicates(subset=['A','B'])
#same as
#df1 = df.drop_duplicates(subset=['A','B'], keep='first')
print (df1)
   A  B  C
0  1  2  x
2  3  4  z
3  3  5  x

df2 = df.drop_duplicates(subset=['A','B'], keep='last')
print (df2)
   A  B  C
1  1  2  y
2  3  4  z
3  3  5  x
Sign up to request clarification or add additional context in comments.

2 Comments

@ jezrael If I want to remove duplicates just from a column without removing rows. Let's say I have 10 rows which is corrowponds to same time instant . So I want to write to txt file but only one time instant I want to print for all 10 rows instead of showing same time for each rows.
@Poka - If dont want remove rows, only solution is replace duplicated values to NaN or empty string. Something like this solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.