I'm analyzing a data set with information from soccer players. I have the soccer player name, his club and all skills. I want to get the mean value of all players from a club and check the skill in which the club is better. For instance, what club has the faster players, the tallest players, etc.
This is what my data looks like:
import pandas as pd
df = pd.DataFrame(
{
"Club": ["Palmeiras", "SPFC", "Corinthians", "Palmeiras", "SPFC"],
"Player": ["FFFFF", "EEEE", "DDDD", "CCCC", "BBBB"],
"Balance": [70, 80, 90, 50, 60],
"Speed": [90, 89, 70, 88, 80],
"Aggression": [70, 74, 80, 85, 66],
}
)
In this example, I get the club with the highest average speed:
print("Club with highest speed: " + df.groupby("Club")["Speed"].mean().reset_index().sort_values("Speed", ascending=False).iloc[0, 0])
I want to print the same thing for all skills, the club with highest speed, the club with the highest balance and so on. I thought I could use something similar to what I did other times with df.iterrows()
, however, I'm having a hard time combining this with the groupby
function.
I've also found this example How to loop over grouped Pandas dataframe?, but it didn't work for me.