2016-07-07 58 views
1

我有SQL statment这样的:Python的大熊猫:在AGG功能case语句

select id 
     , avg(case when rate=1 then rate end) as "P_Rate" 
     , stddev(case when rate=1 then rate end) as "std P_Rate", 
     , avg(case when f_rate = 1 then f_rate else 0 end) as "A_Rate" 
     , stddev(case when f_rate = 1 then f_rate else 0 end) as "std A_Rate" 
from (
select id, connected_date,payment_type,acc_type, 
    max(case when is s_rate > 1 then 1 else 0 end)/count(open) as rate 
    sum(case when is hire_days <= 5 and paid>1000 then 1 else 0 end)/count(open) as f_rate 
from analysis_table where alloc_date <= '2016-01-01' group by 1,2 
) a group by id 

我试图用熊猫改写: 起初我将创建 “内部” 表数据框:

filtered_data = data.where(data['alloc_date'] <= analysis_date) 

然后我就这组数据

grouped = filtered_data.groupby(['id','connected_date']) 

但我必须使用用于过滤每一列使用最大/总和就可以了。

我想是这样的:

`def my_agg_function(hire_days,paid,open): 
    r_arr = [] 
    if hire_days <= 5 and paid > 1000: 
     r_arr.append(1) 
    else: 
     r.append(0) 
    return np.max(r_arr)/len(????) 
inner_table['f_rate'] = grouped.agg(lambda row: my_agg_function(row['hire_days'],row['paid'],row['open'])` 

和速度

回答

1

你应该把你的问题有点数据帧,使其更容易回答类似的东西。

为了您的需要,您可能需要使用groupby dataframes的agg方法。让我们假设你有以下数据框:

connected_date id  number_of_clicks time_spent 
0 Mon    matt 15     124 
1 Tue    john 13     986 
2 Mon    matt 48     451 
3 Thu    jack 68     234 
4 Sun    john 52     976 
5 Sat    sabrina 13     156 

而且你想获得的由用户每天在单个会话的最大点击所花费的时间的总和。然后你使用groupby这样:

df.groupby(['id','connected_date'],as_index = False).agg({'number_of_clicks':max,'time_spent':sum}) 

输出:

id  connected_date time_spent number_of_clicks 
0 jack Thu    234   68 
1 john Sun    976   52 
2 john Tue    986   13 
3 matt Mon    575   48 
4 sabrina Sat    156   13 

请注意,我只通过了as_index=False对输出的清晰度。

+0

好吧让我们看看点击次数看起来​​像(.023,1.2,0.4,2.4,2.1,.1,2),并且U想要计算总和但不像(.023 +1,2等),但是如果number_of_clicks <1 then 0 else 1 and after this calculation sum(1 + 1 + 0 + 1 ..) – gostin

+0

然后在groupby之前做类似下面的事情:'df ['number_of_clicks'] = df ['number_of_clicks']> = 1' 。你会得到boolean的'Series'(它也是0和1到python),groupby中的和会给你你想要的。 – ysearka