2018.03.29 python-pandas transform/apply 的使用
1 #一般化的groupby方法:apply 2 df = pd.DataFrame({'data1':np.random.rand(5), 3 'data2':np.random.rand(5), 4 'key1':list('aabba'), 5 'key2':['one','two','one','two','one']}) 6 print(df) 7 #print(df.groupby('key1').apply(lambda x:x.describe())) 8 #apply直接运算其中的函数 9 #这里是匿名函数,直接描述分组后的统计量 10 11 def f_df1(d,n): 12 return(d.sort_index()[:n]) #返回排序后的前n行数据 13 def f_df2(d,k1): 14 return(d[k1])#返回分组后表的k1列,结果为series,层次化索引 15 print(df.groupby('key1').apply(f_df1,2),'\n')#引入自己创建的函数,注意书写格式,f_df1的第一个参数默认df 第二个参数,2的形式 16 print(df.groupby('key1').apply(f_df2,'data2')) 17 #直接运行f_df函数 18 #参数直接写在后面,也可以写为:.apply(f_df,n=2)
结果:
data1 data2 key1 key2
0 0.253473 0.731034 a one
1 0.531445 0.061414 a two
2 0.733180 0.122293 b one
3 0.550177 0.115940 b two
4 0.101753 0.510581 a one
data1 data2 key1 key2
key1
a 0 0.253473 0.731034 a one
1 0.531445 0.061414 a two
b 2 0.733180 0.122293 b one
3 0.550177 0.115940 b two
key1
a 0 0.731034
1 0.061414
4 0.510581
b 2 0.122293
3 0.115940
Name: data2, dtype: float64
1 # 数据分组转换 .transform 2 import numpy as np 3 import pandas as pd 4 df = pd.DataFrame({'data1':np.random.rand(5), 5 'data2':np.random.rand(5), 6 'key1':list('aabba'), 7 'key2':['one','two','one','two','one']}) 8 print(df) 9 k_mean = df.groupby('key1').mean() 10 print(k_mean) 11 print(pd.merge(df,k_mean,left_on = 'key1',right_index = True).add_prefix('mean_'))#.add_profix('mean_'):增添前缀 12 print('------') 13 #通过分组,合并,得到一个包含均值的Dataframe 以key1位分组的列均值 14 15 print(df.groupby('key2').mean())#按照key2分组求均值 16 print(df.groupby('key2').transform(np.mean)) 17 #data1,data2每个位置的元素取对应分组列的均值 18 #字符串不能进行计算
结果:
data1 data2 key1 key2
0 0.845365 0.411704 a one
1 0.300226 0.411719 a two
2 0.476632 0.628493 b one
3 0.985675 0.304024 b two
4 0.418804 0.229940 a one
data1 data2
key1
a 0.521465 0.351121
b 0.731153 0.466258
mean_data1_x mean_data2_x mean_key1 mean_key2 mean_data1_y mean_data2_y
0 0.845365 0.411704 a one 0.521465 0.351121
1 0.300226 0.411719 a two 0.521465 0.351121
4 0.418804 0.229940 a one 0.521465 0.351121
2 0.476632 0.628493 b one 0.731153 0.466258
3 0.985675 0.304024 b two 0.731153 0.466258
------
data1 data2
key2
one 0.580267 0.423379
two 0.642951 0.357872
data1 data2
0 0.580267 0.423379
1 0.642951 0.357872
2 0.580267 0.423379
3 0.642951 0.357872
4 0.580267 0.423379