データサイエンス100本ノックで勉強(25)

データサイエンス100本ノックでSQLPythonを勉強していきます。

github.com

S-027: レシート明細テーブル(receipt)に対し、店舗コード(store_cd)ごとに売上金額(amount)の平均を計算し、降順でTOP5を表示せよ。

SQLでは以下のようになります。

select store_cd, avg(amount) as amount 
from receipt
group by store_cd
order by amount desc
limit 5;

f:id:JunpeiNakasone:20220215054122p:plain

Pythonでは以下のようになります。

df_receipt.groupby('store_cd').agg({'amount':'mean'}).reset_index(). \
            sort_values('amount',ascending=False).head(5)

f:id:JunpeiNakasone:20220215054538p:plain