データサイエンス100本ノックでSQLとPythonを勉強していきます。
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;
Pythonでは以下のようになります。
df_receipt.groupby('store_cd').agg({'amount':'mean'}).reset_index(). \ sort_values('amount',ascending=False).head(5)