1

I have Comp(PL,Bundesliga etc) column and gls column and player column

standard.groupby(['Comp'])['Gls'].max()

By using this I can max goals in each league

Comp

de Bundesliga         41

eng Premier League    23

es La Liga            30

fr Ligue 1            27

it Serie A            29

But how can I display the player's name who has scored this goal?

I have used this ,

f = {'Gls': 'max','Player': 'first'} 
standard.groupby(['Comp'], as_index=False).agg(f)

It still wont give the desired result, as it just displays first names from each league

    Comp            Gls Player

0   de Bundesliga   41  Issah Abbas\Issah-Abbas

1   eng Premier League  23  Patrick van Aanholt\Patrick-van-Aanholt

2   es La Liga  30  Sabit Abdulai\Sabit-Abdulai

3   fr Ligue 1  27  Ismael Aaneba\Ismael-Aaneba

4   it Serie A  29  Gennaro Acampora\Gennaro-Acampora
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53
adadadad
  • 71
  • 1
  • 7
  • 2
    Does this answer your question? [Pandas groupby and aggregation output should include all the original columns (including the ones not aggregated on)](https://stackoverflow.com/questions/47360510/pandas-groupby-and-aggregation-output-should-include-all-the-original-columns-i) – ThePyGuy Jun 25 '21 at 06:21
  • I have used this ,f = {'Gls': 'max','Player': 'first'} standard.groupby(['Comp'], as_index=False).agg(f) – adadadad Jun 25 '21 at 06:44
  • @adadadad, It would be good if you could place the Dataframe Structure on the Post So, other can understand and reproduce your actual or sample data to showcase you the results you need. – Karn Kumar Jun 25 '21 at 07:10

2 Answers2

1

You can go for idxmax to find the indices of those maximum and loc the frame with those to get also the player information:

standard.loc[standard.groupby("Comp").Gls.idxmax()]
Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
  • @adadadad Sorry, it was an extra term left there. It also works because `to_numpy` gives the values of the series e.g., `[0, 4, 2]` without the keys (index). But since we are indexing with a series (that guy in the square brackets), it's values are used anyway and `to_numpy` isn't necessary. – Mustafa Aydın Jun 25 '21 at 06:54
0

You can try Below:

You will get your actual dataframe.

standard.groupby(['Comp'])['Gls'].transform(max) == df['Gls']

OR

standard[standard['Gls'] == standard.groupby(['comp'])['Gls'].transform(max)]

OR

Using nlargest

standard.groupby(['Comp']).apply(lambda x: x.nlargest(1, "Gls")).reset_index(drop=True)
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53