The Python Oracle

How to drop duplicates from a subset of rows in a pandas dataframe?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5

--

Chapters
00:00 How To Drop Duplicates From A Subset Of Rows In A Pandas Dataframe?
00:38 Accepted Answer Score 11
00:52 Answer 2 Score 5
00:57 Thank you

--

Full question
https://stackoverflow.com/questions/4893...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 11


You can sue pd.concat split the df by B

df=pd.concat([df.loc[df.B!=True],df.loc[df.B==True].drop_duplicates(['A'],keep='first')]).sort_index()
df

Out[1593]: 
    A     B  C
0  12  True  1
2   3   NaN  2
3   3   NaN  3



ANSWER 2

Score 5


df[df.B.ne(True) | ~df.A.duplicated()]

    A     B  C
0  12  True  1
2   3   NaN  2
3   3   NaN  3