Eduardo Eduardo - 1 month ago 15
Python Question

Pandas, filter max values by row and columns

I have this dataframe.

In [6]: df
Out[6]:
Beam Pos Comb As
0 B1 1 1 3
1 B1 1 1 2
2 B1 2 1 5
3 B1 2 1 8
4 B1 1 2 10
5 B1 1 2 1
6 B1 2 2 3
7 B1 2 2 2
8 B2 1 1 1
9 B2 1 1 2
10 B2 2 1 5
11 B2 2 1 6
12 B2 1 2 8
13 B2 1 2 1
14 B2 2 2 3
15 B2 2 2 2


I need to get the max value per beam and position searching in diferent combinations.

Beam Pos Comb As
0 B1 1 2 10
1 B1 2 1 8
2 B2 1 2 8
3 B2 2 1 6


I can't figured out how can I compare the "As" values for Beam, position and combination.

Perhaps grouping the beam, the positions and then get the max value?

Any help will be appreciated

Answer

How about this?

groups = df.groupby(by=['Beam', "Pos"])
idx = []
for group in groups:
    idx += [group[1].As.argmax()]

than show df.iloc[idx]