How to remove duplicates from a subset of rows in pandas framework?

I have a dataframe like this:

A   B       C
12  true    1
12  true    1
3   nan     2
3   nan     3

I would like to delete all the rows where the value of column A will be repeated, but only if the value of column B is "true".

The resulting information frame, which I mean, is:

A   B       C
12  true    1
3   nan     2
3   nan     3

I tried using: df.loc[df['B']=='true'].drop_duplicates('A', inplace=True, keep='first')but it does not work.

Thank you for your help!

+4
source share
2 answers

You can sue pd.concatdivide 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
+4
source
df[df.B.ne(True) | ~df.A.duplicated()]

    A     B  C
0  12  True  1
2   3   NaN  2
3   3   NaN  3
+4
source

Source: https://habr.com/ru/post/1694015/


All Articles