0

I have this dataframe and I want to groupby city and country and get the city with higher pop in each group:

df = pd.DataFrame({'country': ['Brasil', 'Brasil' , 'EUA', 'EUA'],
                  'city': ['Santos', 'São Paulo', 'Orlando', 'Nova York'],
                  'pop': [100000, 500000, 200000, 550000]})
df

What I want:

country city pop
Brasil São Paulo 500000
EUA Nova York 550000

What I have already done:

df.groupby(['country','city']).loc[df.pop == df.pop.max()]

Python returns: "AttributeError: Cannot access callable attribute 'loc' of 'DataFrameGroupBy' objects, try using the 'apply' method"

I know that I must use apply, but I still don´t understand the logic of it in python. Does anyone know how to help me?

jvqp
  • 239
  • 3
  • 14

0 Answers0