Groupby用于选择多列Pandas python

时间:2019-09-24 07:53:21

标签: python pandas group-by

我有一个3列的表格pandas数据框df,可以说:

[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

任何人都可以帮忙

2 个答案:

答案 0 :(得分:1)

首先通过比较与&链接的两列按位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-string

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

答案 1 :(得分:1)

或者我们可以使用计数

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
相关问题