Python Forum

Full Version: Select in Multi Index Pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have a table qith this structure:

IdP IdC idE
1 1 1
1 1 4
1 1 4
1 1 5
2 3 2
2 3 1
2 3 2
2 3 2
....

So, every row is an entry where IdP and IdC are always the same and IdE is the column that changes. Every row is an observatyion (I have hundreds of them for every IdP) and I want to compute the count of idE for every IdP. I alredy has done that with:

agg = df.groupby(['IdP', 'IdP', 'IdC']).agg({'Predicted': ['count']})

That gives me a DataFrame with this structure:

Predicted
count
IdP IdP IdC
10 1 1 63
4 1 1
12 1 5 6
3 5 4
4 5 1
5 5 52

And now, what I need, and don't figure out how to do it, is to keep only the rows where count is the max within IdP, so in this example I would get:

Predicted
count
IdP IdP IdC
10 1 1 63
12 5 5 52

I really don't need to get the count in the final table, just the IdP that gioes with him.

I am trying to do more agrregations and groupsby in pandas but I am not able to get he good result.

Thanks