用于选择多列的Groupby

2024-05-28 18:48:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧df表,有3列,比如:

[IN]:df
[OUT]:

Tree Name   Planted by Govt   Planted by College
A               Yes                No 
B               Yes                No
C               Yes                No 
C               Yes                No
A               No                 No 
B               No                 Yes
B               Yes                Yes
B               Yes                No
B               Yes                No  

查询:

每种树有多少棵是由政府种植的,而不是由大学种植的。政府:是,私人:否

需要输出:

1 Tree(s) 'A' were planted by govt and not by college
3 Tree(s) 'B' were planted by govt and not by college
2 Tree(s) 'C' were planted by govt and not by college

有人能帮忙吗


Tags: and数据nointreedfbynot
2条回答

或者我们可以用count

df[df['Planted by Govt'].eq('Yes')& df['Planted by College'].eq('No')].groupby('Tree Name').count()['Planted by Govt'].rename('PLanted only by Govt')

print(result)
Tree Name
A    1
B    3
C    2
Name: PLanted only by Govt, dtype: int64

首先通过比较按位AND&链接的两个列来创建布尔掩码,然后用聚合sum转换为数字:

s = df['Planted by Govt'].eq('Yes') & df['Planted by College'].eq('No')
out = s.view('i1').groupby(df['Tree Name']).sum()
#alternative
#out = s.astype(int).groupby(df['Tree Name']).sum()
print (out)
Tree Name
A    1
B    3
C    2
dtype: int8

最后一个自定义输出使用f-strings:

for k, v in out.items():
    print (f"{v} Tree(s) {k} were planted by govt and not by college")

    1 Tree(s) A were planted by govt and not by college
    3 Tree(s) B were planted by govt and not by college
    2 Tree(s) C were planted by govt and not by college

另一个想法是根据原文创建新专栏:

df['new'] = (df['Planted by Govt'].eq('Yes') & df['Planted by College'].eq('No')).view('i1')
print (df)
  Tree Name Planted by Govt Planted by College  new
0         A             Yes                 No    1
1         B             Yes                 No    1
2         C             Yes                 No    1
3         C             Yes                 No    1
4         A              No                 No    0
5         B              No                Yes    0
6         B             Yes                Yes    0
7         B             Yes                 No    1
8         B             Yes                 No    1

out = df.groupby('Tree Name')['new'].sum()
print (out)
Tree Name
A    1
B    3
C    2
Name: new, dtype: int8

相关问题 更多 >

    热门问题