I have a pandas dataframe with approx. 1200 rows, where some of the rows are duplicated multiple times. The df looks like this:
ID Serial Age Grade Chem Bio Math Phy
M001 2 52 37 1 1 1 1
M001 2 55 37 2 1 0 1
M001 3 51 36,5 1 1 1 0
M001 3 51 46,5 1 0 1 1
M041 2 52 36,1 1 1 0 0
M041 2 51 36,1 2 1 2 4
M041 2 52 36,1 1 1 0
M041 2 52 36,1 1 1 1
M010 5 58 37,4 0 1 1 3
M010 5 55 39,4 1 2 1 1
M010 5 58 37,4 1 1 1 1
The duplicates in the dataframe are supposed to be identified by the ID and Serial column and I was able to do that. However, I would like to compare each group of ID+Serial rows to find where they differ. This is a bit tricky as sometimes there are 2 rows in a group to compare and sometimes the rows (to compare) are more than 2 within each group.
I am interested in a solution where I can groupby the dataframe based on ID and Serial cols and then compare rows within each group. If there is a difference between two or more cells that needs to be recorded (e.g., in a new row with a X below the conflicting cells or perhaps highlighting the cells in red color). The resulting dataframe should look something like this:
ID Serial Age Grade Chem Bio Math Phy
M001 2 52 37 1 1 1 1
M001 2 55 37 2 1 0 1
M001 2 X X X
M001 3 51 36,5 1 1 1 0
M001 3 51 46,5 1 0 1 1
M001 3 X X X
M041 2 52 36,1 1 1 0 0
M041 2 51 36,1 2 1 2 4
M041 2 52 36,1 1 1 0
M041 2 52 36,1 1 1 1
M041 2 X X X X
M010 5 58 37,4 0 1 1 3
M010 5 55 39,4 1 2 1 1
M010 5 58 37,4 1 1 1 1
M010 5 X X X X X
Can someone help with this issue?