Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在条件上对数据帧进行分组?_Python_Pandas_Group By - Fatal编程技术网

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