name sex height weight
a m 175 150
b m 180 130
c f 160 100
d f 155 90
我要计算不同性别的平均身高,平均体重,身高/体重比 并且放在一个结果中
用dplyr我可以很简单的直接用
- df%>%group_by(sex)%>% summarize(mean_weight=mean(weight),mean_height=mean(height),height_weight=height/weight)
来让他们的结果作为一个数据框返回
但是用python的groupby 我最多只能同时返回身高和体重的均值,像这种要做额外计算的话我提前需要自定义一个函数function,并且最终也只能返回一个字典
- def function(df):
- weight=mean(df.weight)
- height=mean(df.height)
- weight_height=mean(weight/height)
- return({'mean_weight':weight,'mean_height':mean_height,'weight_height':weight_height})


雷达卡




京公网安备 11010802022788号







