找到鸟鹤的总访问量吗?
程序:
import pandas as pd
import numpy as np
df = pd.DataFrame({'birds': ['Cranes', 'Cranes', 'plovers', 'spoonbills', 'spoonbills', 'Cranes', 'plovers', 'Cranes', 'spoonbills', 'spoonbills'], 'age': [3.5, 4, 1.5, np.nan, 6, 3, 5.5, np.nan, 8, 4], 'visits': [2, 4, 3, 4, 3, 4, 2, 2, 3, 2], 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']})
g=df.groupby(['birds','visits']).sum()
g
输出:
age
birds visits
Cranes 2 3.5
4 7.0
plovers 2 5.5
3 1.5
spoonbills 2 4.0
3 14.0
4 0.0
所需的输出:
birds visits
Cranes 2
Cranes 4
Cranes 4
Cranes 2
Total 12
还要让我知道如何删除即将到来的年龄列吗?
如果我尝试使用groupby,其他列也将出现,并且结果不明确
答案 0 :(得分:0)
将熊猫作为pd导入 将numpy导入为np
df = pd.DataFrame({'birds':['Cranes','Cranes','plovers','spoonbills','spoonbills,'Cranes','plovers','Cranes','spoonbills' ,“琵鹭”,“年龄”:[3.5、4、1.5,np.nan,6、3、5.5,np.nan,8、4],“访问”:[2、4、3、4、3 ,4,2,2,3,2],'priority':['yes','yes','no','yes','no','no','no','yes','否','否']})
g = df.groupby(['birds'])。sum() g ['visits']