我有一张如下所示的表格:
City_code City_name Site_code Site_capacity
AAA100 City_A Site001 300
AAA100 City_A Site002 600
AAA100 City_A Site003 500
AAA200 City_B Site004 350
AAA200 City_B Site005 250
AAA300 City_C Site006 800
AAA300 City_C Site007 150
AAA300 City_C Site008 450
AAA400 City_D Site009 300
AAA400 City_D Site0010 400
我想为每个城市选择 Site_capacity 值最高的站点
我尝试了以下代码:
df.groupby(['City_code', 'City_name'])['Site_capacity'].max()
这是它生成的输出:
City_code City_name
AAA100 City_A 600
AAA200 City_B 350
AAA300 City_C 800
AAA400 City_D 400
我如何创建一些看起来像这样的输出?
City_code City_name Site_code Site_capacity
AAA100 City_A Site002 600
AAA200 City_B Site004 350
AAA300 City_C Site006 800
AAA400 City_D Site0010 400