【发布时间】:2018-06-20 14:48:23
【问题描述】:
我有以下数据框:
df = pd.DataFrame({'place' : ['A', 'B', 'C', 'D', 'E', 'F'],
'population': [10 , 20, 30, 15, 25, 35],
'region': ['I', 'II', 'III', 'I', 'II', 'III']})
它看起来像这样:
place population region
0 A 10 I
1 B 20 II
2 C 30 III
3 D 15 I
4 E 25 II
5 F 35 III
我想从人口最多的地区中选择人口最少的地方。
df.groupby('region').population.sum()
返回:
region
I 25
II 45
III 65
Name: population, dtype: int64
但我不知道如何从这里开始(使用 .groupby / .loc / .iloc)
有什么建议吗?
【问题讨论】:
标签: python python-3.x pandas dataframe pandas-groupby