Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas:drop_duplicates() based on condition in python

Tags:

python

pandas

Having below data set:

data_input:

    A     B
1  C13D  C07H
2  C07H  C13D
3  B42C  B65H
4  B65H  B42C
5  A45B  A47C

i.e. row 1 and row 2 in data_input are same,I just want to keep one,so drop row 2.

Want the Output as below:

data_output:

    A     B
1  C13D  C07H
2  B42C  B65H
3  A45B  A47C
like image 775
littlewilliam Avatar asked Mar 14 '23 16:03

littlewilliam


1 Answers

You can create a third column 'C' based on 'A' and 'B' and use it to find duplicates as such:

df['C'] = df['A'] + df['B']
df['C'] = df['C'].apply(lambda x: ''.join(sorted(x)))
df = df.drop_duplicates(subset='C')[['A', 'B']]
like image 178
ranlot Avatar answered Mar 22 '23 23:03

ranlot