【问题标题】:passing parameters in groupby aggregate function在 groupby 聚合函数中传递参数
【发布时间】:2019-01-22 00:17:59
【问题描述】:

我有我在代码中引用为df 的数据框,并且我在每个组的多个列上应用聚合函数。我还应用了用户定义的 lambda 函数f4, f5, f6, f7。一些函数非常相似,例如f4, f6 和f7,只有参数值不同。我可以从 dictionary d 传递这些参数,这样我就只需要编写一个函数而不是编写多个函数吗?

f4 = lambda x: len(x[x>10]) # count the frequency of bearing greater than threshold value
f4.__name__ = 'Frequency'

f5 = lambda x: len(x[x<3.4]) # count the stop points with velocity less than threshold value 3.4
f5.__name__ = 'stop_frequency'

f6 = lambda x: len(x[x>0.2]) # count the points with velocity greater than threshold value 0.2
f6.__name__ = 'frequency'

f7 = lambda x: len(x[x>0.25]) # count the points with accelration greater than threshold value 0.25
f7.__name__ = 'frequency'

d = {'acceleration':['mean', 'median', 'min'], 
 'velocity':[f5, 'sum' ,'count', 'median', 'min'], 
 'velocity_rate':f6,
 'acc_rate':f7,
 'bearing':['sum', f4], 
 'bearing_rate':'sum',     
 'Vincenty_distance':'sum'}

df1 = df.groupby(['userid','trip_id','Transportation_Mode','segmentid'], sort=False).agg(d)

#flatenning MultiIndex in columns
df1.columns = df1.columns.map('_'.join)
#MultiIndex in index to columns
df1 = df1.reset_index(level=2, drop=False).reset_index()

我喜欢写这样的函数

f4(p) = lambda x: len(x[x>p]) 
f4.__name__ = 'Frequency'

d = {'acceleration':['mean', 'median', 'min'], 
 'velocity':[f5, 'sum' ,'count', 'median', 'min'], 
 'velocity_rate':f4(0.2),
 'acc_rate':f4(0.25),
 'bearing':['sum', f4(10)], 
 'bearing_rate':'sum',     
 'Vincenty_distance':'sum'}

数据框 df 的 csv 文件可在给定的链接中获得,以使数据更加清晰。 https://drive.google.com/open?id=1R_BBL00G_Dlo-6yrovYJp5zEYLwlMPi9

【问题讨论】:

  • 嗯,这可以通过在所有行中使用单个 apply 函数来实现。然后从d 访问参数。但是您必须创建一个用户定义的函数并在apply 中使用它,包括更多代码。
  • 你能给我一些有关如何使用字典中的参数调用用户定义函数的相关示例吗?
  • 'bearing':['sum', f10(0.2)], def f10(p): return len(x[x>p]) ... 困惑我们如何传递 x在 lambda 中
  • 你需要为每一列/大部分列应用它吗?

标签: python-3.x pandas pandas-groupby


【解决方案1】:

neilaronson 的解决方案是可能的,但并不容易。

另外解决方案是通过布尔掩码的sum 的True 值来简化。

def f4(p):
    def ipf(x):
        return (x < p).sum()
        #your solution
        #return len(x[x < p])
    ipf.__name__ = 'Frequency'
    return ipf 

d = {'acceleration':['mean', 'median', 'min'], 
 'velocity':[f4(3.4), 'sum' ,'count', 'median', 'min'], 
 'velocity_rate':f4(0.2),
 'acc_rate':f4(.25),
 'bearing':['sum', f4(10)], 
 'bearing_rate':'sum',     
 'Vincenty_distance':'sum'}

df1 = df.groupby(['userid','trip_id','Transportation_Mode','segmentid'], sort=False).agg(d)

#flatenning MultiIndex in columns
df1.columns = df1.columns.map('_'.join)
#MultiIndex in index to columns
df1 = df1.reset_index(level=2, drop=False).reset_index()

编辑:您也可以传递参数以获取更大或更小:

def f4(p, op):
    def ipf(x):
        if op == 'greater':
            return (x > p).sum()
        elif op == 'less':
            return (x < p).sum()  
        else:
            raise ValueError("second argument has to be greater or less only")
    ipf.__name__ = 'Frequency'
    return ipf 



d = {'acceleration':['mean', 'median', 'min'], 
 'velocity':[f4(3.4, 'less'), 'sum' ,'count', 'median', 'min'], 
 'velocity_rate':f4(0.2, 'greater'),
 'acc_rate':f4(.25, 'greater'),
 'bearing':['sum', f4(10, 'greater')], 
 'bearing_rate':'sum',     
 'Vincenty_distance':'sum'}

df1 = df.groupby(['userid','trip_id','Transportation_Mode','segmentid'], sort=False).agg(d)

#flatenning MultiIndex in columns
df1.columns = df1.columns.map('_'.join)
#MultiIndex in index to columns
df1 = df1.reset_index(level=2, drop=False).reset_index()

print (df1.head())
   userid  trip_id  segmentid Transportation_Mode  acceleration_mean  \
0     141      1.0          1                walk           0.061083   
1     141      2.0          1                walk           0.109148   
2     141      3.0          1                walk           0.106771   
3     141      4.0          1                walk           0.141180   
4     141      5.0          1                walk           1.147157   

   acceleration_median  acceleration_min  velocity_Frequency  velocity_sum  \
0        -1.168583e-02         -2.994428              1000.0   1506.679506   
1         1.665535e-09         -3.234188               464.0    712.429005   
2        -3.055414e-08         -3.131293               996.0   1394.746071   
3         9.241707e-09         -3.307262               340.0    513.461259   
4        -2.609489e-02         -3.190424               493.0    729.702854   

   velocity_count  velocity_median  velocity_min  velocity_rate_Frequency  \
0            1028         1.294657      0.284747                    288.0   
1             486         1.189650      0.284725                    134.0   
2            1020         1.241419      0.284733                    301.0   
3             352         1.326324      0.339590                     93.0   
4             504         1.247868      0.284740                    168.0   

   acc_rate_Frequency   bearing_sum  bearing_Frequency  bearing_rate_sum  \
0               169.0  81604.187066              884.0       -371.276356   
1                89.0  25559.589869              313.0       -357.869944   
2               203.0 -71540.141199               57.0        946.382581   
3                78.0   9548.920765              167.0       -943.184805   
4                93.0 -24021.555784               67.0        535.333624   

   Vincenty_distance_sum  
0            1506.679506  
1             712.429005  
2            1395.328768  
3             513.461259  
4             731.823664  

【讨论】:

    猜你喜欢
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多