-1

I am trying to drop duplicate values at column P but according to column S values. I meant that every group in S columns. Like according to S group there are four groups 1,2,3 and 4. and so according to first group i am trying to find values 5,7,6 and 1st index should drop.

    S  A4  P
0   1   1  5
1   1   2  5
2   1   3  7
3   1   4  6
4   2   1  7
5   2   2  6
6   2   3  7
7   2   1  1
8   3   5  2
9   3   3  3
10  3   2  4
11  3   1  1
12  4   5  2
13  4   3  3
14  4   5  4
15  4   6  5

So every duplicated values at P column according to S group not wanted. This is the searching df that i am trying to find:

Search
    S  A4  P
0   1   1  5
1   1   3  7
2   1   4  6
3   2   1  7
4   2   2  6
5   2   1  1
6   3   5  2
7   3   3  3
8   3   2  4
9   3   1  1
10  4   5  2
11  4   3  3
12  4   5  5

1 Answers1

1

Use drop_duplicates() with subset and keep='first':

In [2335]: df.drop_duplicates(sub['S', 'P'], keep='first')
Out[2335]: 
    S  A4  P
0   1   1  5
2   1   3  7
3   1   4  6
4   2   1  7
5   2   2  6
7   2   1  1
8   3   5  2
9   3   3  3
10  3   2  4
11  3   1  1
12  4   5  2
13  4   3  3
14  4   5  4
15  4   6  5
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58