【发布时间】:2021-12-25 00:14:07
【问题描述】:
我的数据集的 sn-p 如下所示:
User_id | Search_id | Price | score | clicked | company | rank
1 | 1 | 10 | 7.3 | 0 | other | 3
1 | 1 | 8 | 10.0 | 1 | other | 2
1 | 1 | 7.5 | 10.0 | 1 | us | 1
2 | 2 | 7 | 10.0 | 0 | us | 3
2 | 2 | 6.5 | 10.0 | 1 | other | 2
2 | 2 | 4 | 6.5 | 1 | other | 1
我使用的 SQL 查询类似于:
proc sql;
create table File1_Real as
select User_ID
, Search_ID
, mean(case when company = 'us' then rank else . end) as Our_Rank
, mean(case when company = 'us' then Price else . end) as Our_Price
, mean(case when company = 'us' then score else . end) as Our_Score
, mean(case when company = 'us' then clicked else . end) as Our_Click
, mean(case when rank <= 5 then price else . end) as Price_Top5
, mean(case when Rank = BestClickRank then price else . end) as Top_Price
, mean(case when Rank = BestClickRank then case when score= 10 then 1 else 0 end else . end) as Top_is10
, mean(BestClickRank) as Top_Rank
, min(price) as Min_Price
, count(*) as QuotesReturned
, sum(case when price < 7.5 then 1 else 0 end) as QuotesLT75
, mean(case when price < 7.5 then Score else 0 end) as LT75_Score
, sum(clicked) as TotalClicks
from (
Select *
, min(case when Clicked = 1 then Rank else . end) as BestClickRank
from work.data
where score = 10
group by user_id, search_id)
Group by 1,2
quit;
预期的输出类似于:
User_id | Search_id | Our_Rank| Our_Price | Our_Score | Our_Click | Price_Top5 | Top_Price | Top_is10 | Top_Rank | Min_Price | QuotesReturned | QuotesLT75 | LT75_Score | TotalClicks
1 | 1 | 1 | 7.5 | 10.0 | 1 | 7.75 | 7.5 | 1 | 1 | 7.5 | 2 | 0 | 0 | 2
2 | 2 | 3 | 7 | 10.0 | 0 | 5.8 | 6.5 | 1 | 2 | 6.5 | 2 | 0 | 0 | 1
我尝试过这样的事情:
df[(df['company']!='us')].groupby(['User_id','Search_id']).agg({'price':['min','mean']})
但是:1) 它不返回其余列,并且 2) 我不确定如何进行计算以返回另一列中的值,例如 Our_Rank。
有没有可能一起做这一切? 我无权访问 SQL-pandas 包,因此只能使用 pandas。
【问题讨论】:
-
你的预期输出是什么?
-
@not_speshal 刚刚添加 :)