本文主要是介绍熊猫 分组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import pandas as pd
import numpy as np
group by : split apply combine
dic_gpa = {‘class’:[‘A’,‘B’,‘A’,‘B’],
‘sname’:[‘tom’,‘kite’,‘tom’,‘hanmeimei’],
‘math’:(np.random.rand(4)*100).round(),
‘english’:(np.random.rand(4)*100).round()
}
df = pd.DataFrame(dic_gpa)
smean_byclass = df.groupby([‘class’]).mean()
smean_byname = df.groupby([‘sname’]).mean()
lambda_name = df.groupby(lambda x:‘even class’ if x%2==0 else ‘odd class’).mean()
agg_byClassSname = df.groupby([‘class’,‘sname’]).agg([np.mean,np.min])
agg_byClassSname_different = df.groupby([‘class’,‘sname’]).agg({‘math’:np.mean,‘english’:np.max})
mean_minus = df.groupby(‘class’).apply(lambda x:x[‘math’].mean()-x[‘english’].mean())
transformer = df.groupby(‘class’).transform(np.mean)
combine_concat = pd.concat([df,transformer],axis=1)
print(df)
print(’*’*100)
#print(smean_byclass)
print(smean_byclass)
combine_merge = pd.merge(df,smean_byclass,how=‘left’,on=[‘class’])
print(combine_merge)
这篇关于熊猫 分组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!