pandas 是否可以使用“NamedAgg”方法进行过滤?
这是我的示例代码:
df = pd.DataFrame({'Person': ['John','Paul','John','Paul','Taylor'],
'animal': ['cat', 'dog', 'cat', 'dog','dog'],
'from' : ['breeder','adoption','adoption','breeder','wild'],
'height': [9.1, 6.0, 9.5, 34.0,55],
'weight': [7.9, 7.5, 9.9, 198.0,200]})
df.groupby(['Person']).agg(
number_of_animal = pd.NamedAgg(column = 'animal', aggfunc = 'count'),
number_of_from = pd.NamedAgg(column = 'from', aggfunc = 'count'),
total_height = pd.NamedAgg(column = 'height', aggfunc = 'sum'),
total_weight = pd.NamedAgg(column = 'weight', aggfunc = 'sum')
)
result = pd.DataFrame({'Person': ['John','Paul','Taylor'],
'number_of_animal':[2,0,0],
'number_of_from': [1,1,0],
'total_height':[0,34,55],
'total_weight':[17.8,205.5,200]})
对于每个单独的列,我想应用一个过滤器,例如我想过滤“number_of_animal”df['animal'] == 'cat'和“total_hight”df['height'] > 10和 number_of_fromdf['from'] == 'breeder
一只名叫tom的猫
相关分类