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
  [split] Getting Index Error - list index out of range krishna 2 1,026 Jan-09-2021, 08:29 AM
Last Post: buran
  Getting Index Error - list index out of range RahulSingh 2 3,377 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 1,264 Jan-28-2020, 09:30 AM
Last Post: axelle
  How to speed up work with pandas index? AlekseyPython 1 1,041 Oct-16-2019, 02:06 PM
Last Post: AlekseyPython
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 1,499 Sep-04-2019, 10:04 PM
Last Post: Nuovoq
  pandas: can we look for the index of a string paul18fr 2 1,224 Jul-31-2019, 08:25 AM
Last Post: paul18fr
  Select specific index for plot rundesquadrat 1 1,244 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 3,196 Oct-13-2018, 07:04 AM
Last Post: perfringo
  Using pandas, index error fyec 1 3,126 Aug-01-2018, 09:25 AM
Last Post: volcano63
  is a pandas dataframe timeseries time index in a specified range (but ignoring date)? m_lotinga 4 16,496 Dec-12-2016, 10:51 PM
Last Post: m_lotinga

Forum Jump:

User Panel Messages

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