Python 如何在条件上对数据帧进行分组?
我想在一个数据库上使用一个有条件的Python 如何在条件上对数据帧进行分组?,python,pandas,group-by,Python,Pandas,Group By,我想在一个数据库上使用一个有条件的groupby test = pd.DataFrame({'A':range(9), 'B':['this','this','this','that','and','the','other','thing','.']}) 根据“B”是否为“this”(我如何想象这个w/A查询结构)对“A”进行分组: 应返回此:[3,33] pd.Series([3,33]) 谢谢,如果您将另一个长度相同的序列传递给groupby,则groupby将起作用,因此您可以先计算条
groupby
test = pd.DataFrame({'A':range(9), 'B':['this','this','this','that','and','the','other','thing','.']})
根据“B”是否为“this”(我如何想象这个w/A查询结构)对“A”进行分组:
应返回此:[3,33]
pd.Series([3,33])
谢谢,如果您将另一个长度相同的序列传递给groupby,则groupby将起作用,因此您可以先计算条件序列,然后再按其分组:
test.groupby(test.B == "this").sum()
# A
# B
#False 33
# True 3
test.groupby(test.B == "this").sum()
# A
# B
#False 33
# True 3