[英]Group and Compare multiple dataframe columns with conditions in Python
I'm tryng to print out the states with highest population in each region.我正在尝试打印出每个地区人口最多的州。
Code Sample :代码示例:
# all unique regions
region_unique = data['Region'].unique()
# highest population
max_pop = data['population'].max()
How can I chain the above lines of code and bring in the 'States'
column to achieve my result?如何链接上述代码行并引入
'States'
列以实现我的结果?
Dataset :数据集:
Considering you haven't mentioned any library...考虑到你没有提到任何图书馆......
You could first create a helper dict , mapping each region to an array of states.您可以首先创建一个辅助dict ,将每个区域映射到一个状态数组。 Each state is a tuple:
(state, pop)
(name and population count):每个 state 是一个元组:(
(state, pop)
(名称和人口计数):
regions = {}
for state, pop, region in zip(data['States'], data['population'], data['Region']):
res.setdefault(region, []).append((state, pop))
Then for each region you can pull out the most inhabited state:然后对于每个区域,您可以提取出最多人居住的 state:
for region, states in regions.items():
print(region, max(states, key=lambda _, pop: pop))
To states under each region with a population less than 100 , you can do:对于每个地区下人口少于100的州,您可以执行以下操作:
for region, states in regions.items():
print(region, list(filter(lambda state: state[1] > 100, states)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.