[英]Compute percentage for group using groupby in pandas dataframe
In the following dataframe:在以下 dataframe 中:
country admin_1 admin_2 year season_name production area
A1 B1 C1 1991 Primary 43170 25980
A1 B1 C1 1990 Primary 45624 29820
A1 B1 C1 1989 Primary 56310 31284
A1 B1 C1 1988 Primary 33523 24832
A1 B1 C1 1987 Primary 49388 33479
A1 B1 C1 1986 Primary 36475 27425
A1 B1 C1 1985 Primary 32278 32046
A1 B1 C1 1984 Primary 52073 28929
A1 B1 C1 1983 Primary 51746 32855
A1 B1 C2 1991 Primary 32010 20010
A1 B1 C2 1990 Primary 52704 19520
A1 B1 C2 1989 Primary 65240 18640
A1 B1 C2 1988 Primary 11570 17800
A1 B1 C2 1987 Primary 51282 20350
A1 B1 C2 1986 Primary 25808 19816
A1 B1 C2 1985 Primary 16935 18817
A1 B2 C3 1987 Primary 51282 20350
A1 B2 C3 1986 Primary 25808 19816
A1 B2 C3 1985 Primary 16935 18817
I want to determine the percentage of area for each admin_2 by averaging the area across all years for each admin_2 and them computing the percentage.我想通过平均每个 admin_2 的所有年份的面积来确定每个 admin_2 的面积百分比,然后他们计算百分比。 This is what I tried:这是我尝试过的:
df['area_percentage'] = df.groupby(['country', 'admin_2'])['area'].apply(lambda x: x / x.sum())
Try:尝试:
df['area_percentage'] = df['area'] / df.groupby(['country', 'admin_2'])['area'].transform('sum') * 100
Well, since the question is somewhat vague, I'll do a sort of mental exercise.好吧,由于这个问题有些模糊,我会做一些心理锻炼。
Let's see how we can interpret "the percentage of area for each admin_2 by averaging the area across all years for each admin_2" .让我们看看如何通过平均每个 admin_2 的所有年份的面积来解释“每个 admin_2 的面积百分比” 。
Looking at the first attempt, which is considered wrong, I could come up with something like this:看着第一次尝试,这被认为是错误的,我可以想出这样的事情:
s = df.groupby(['country', 'admin_2'])['area'].mean()
s /= s.sum()
Output: Output:
country admin_2
A1 C1 0.432095
C2 0.281167
C3 0.286738
But it looks weird.但它看起来很奇怪。 What could be the benefit of normalizing means?标准化手段有什么好处? I don't know.我不知道。 So let's skip it and do something different.所以让我们跳过它,做一些不同的事情。
What if we sum up the area
for all the years grouped by admini_2
and divide the result by the total area
?如果我们admini_2
分组的所有年份的area
相加并将结果除以总area
会怎样?
s = df.groupby(['country', 'admin_2'])['area'].sum()
s /= s.sum()
Output: Output:
country admin_2
A1 C1 0.578936
C2 0.293003
C3 0.128061
Well, it might work if the nature of area
implies the accumulation.好吧,如果area
的性质意味着积累,它可能会起作用。 In this case, we can talk about the overall percentage of area
for each admin_2
.在这种情况下,我们可以讨论每个admin_2
的总area
百分比。
But what if there's no "accumulation" on the table?但是,如果桌子上没有“积累”怎么办? Let's say the area means the amount of work that must be done when necessary during the year.假设面积是指一年中必要时必须完成的工作量。 In this case we could interpret the request this way: compare area
for each admin_2
by year
.在这种情况下,我们可以这样解释请求:按year
比较每个admin_2
的area
。 From the data presented I might conclude that country
and admin_1
do not really matter.从提供的数据中,我可能会得出结论, country
和admin_1
并不重要。 They are the same for each admin_2
, so I guess they can be ignored (otherwise, we simply add them to the pivot table index).它们对于每个admin_2
都是相同的,所以我想它们可以被忽略(否则,我们只需将它们添加到 pivot 表索引中)。 In this case, I'd do something like this:在这种情况下,我会做这样的事情:
_df = df.pivot('admin_2','year','area')
_df = (_df / _df.sum()).T
display(_df.fillna(0).style.format('{0:.0%}'.format))
_df.plot(kind='bar'))
Output: Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.