Python Forum
Select in Multi Index Pandas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select in Multi Index Pandas
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 607 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  multi index issue of one hot encoder preprocessing aupres 0 1,057 Jun-10-2022, 11:23 AM
Last Post: aupres
  [split] Getting Index Error - list index out of range krishna 2 2,567 Jan-09-2021, 08:29 AM
Last Post: buran
  Getting Index Error - list index out of range RahulSingh 2 6,101 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 2,549 Jan-28-2020, 09:30 AM
Last Post: axelle
  How to speed up work with pandas index? AlekseyPython 1 2,161 Oct-16-2019, 02:06 PM
Last Post: AlekseyPython
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 2,619 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  pandas: can we look for the index of a string paul18fr 2 2,185 Jul-31-2019, 08:25 AM
Last Post: paul18fr
  Select specific index for plot rundesquadrat 1 2,173 Dec-26-2018, 07:47 AM
Last Post: scidam
  How to get first and last row index of each unique names in pandas dataframe SriRajesh 1 4,451 Oct-13-2018, 07:04 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020