ダウンロードファイルはこちらから
http://www3.shuwasystem.co.jp/support/7980html/5875.html
環境:Google Colab
ノック1:データの読み込み
ノック1.py
import pandas as pd
customer_master=pd.read_csv("custmor_master.csv")
item_master=pd.read_csv("item_master.csv")
transaction_1=pd.read_csv("transaction_1.csv")
transaction_detail_1=pd.read_csv("transaction_detail_1.csv")
ノック2:データを結合(インデックス数を増やす)
ノック2.py
transaction_2=pd.read_csv("transaction_2.csv")
transaction=pd.concat([transaction_1,transaction_2],ignore_index=True)
transaction_detail_2=pd.read_csv("transaction_detail_2.csv")
transaction_detail=pd.concat([transaction_detail_1,transaction_detail_2],ignore_index=True)
ノック3,4:データを結合(カラム数を増やす)
ノック3,4.py
join_data=pd.merge(transaction_detail,transaction[["transaction_id","payment_date","customer_id"]],on="transaction_id",how="left")
join_data=pd.merge(join_data,customer_master,on="customer_id",how="left")
join_data=pd.merge(join_data,item_master,on="item_id",how="left")
howの使い方
https://www.sejuku.net/blog/76325
ノック5:必要なデータ列を作る
ノック5.py
join_data["price"]=join_data["quantity"]*join_data["item_price"]#列の追加
ノック6:データの検算
ノック6.py
print(join_data["price"].sum())
print(transaction["price"].sum())
#又は
join_data["price"].sum() == transaction["price"].sum()#TRUE/FALSEでもok
ノック7:各種統計量を把握
ノック7.py
join_data.isnull().sum()#欠損値の数を表示
join_data.describe()#データ件数(count),平均値(mean),標準偏差(std),最小値(min),四分位数(25%,75%),中央値(50%),最大値(max)を表示
#別個で確認すべき期間範囲データ
print(join_data["payment_date"].min())
print(join_data["payment_date"].max())
ノック8:月別でデータ集計
ノック8.py
join_data["payment_date"]=pd.to_datetime(join_data["payment_date"])#object型→datetime型
join_date["payment_month"]=join_data["payment_date"].dt.strftime("%Y%m")#dtを使って年,月のみを抽出
join_data.groupby("payment_month").sum()["price"]
%Y年%m月%d日%H時%M分で他の情報も得られる
ノック9:月別,商品別でデータ集計
ノック9.py
join_data.groupby(["payment_month","item_name"]).sum()[["price","quantity"]]
pd.pivot_table(join_data,index="item_name",columns="payment_month",values=["price","quantity"],aggfunc="sum")
pivot_tableのindexとcoulumnsは行と列を指定できる!
valuesは集計したい数値列を指定できる!その後、aggfuncで集計方法をsumに設定している!
```
ノック10:商品別の売上推移を可視化
ノック10.py
graph_data=pd.pivot_table("join_data",index="payment_month",columns="item_name",values="price",aggfunc="sum")
ノック10.py
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(list(graph_data.index),graph_data["PC_A"],label="PC_A")
plt.plot(list(graph_data.index),graph_data["PC_B"],label="PC_B")
plt.plot(list(graph_data.index),graph_data["PC_C"],label="PC_C")
plt.plot(list(graph_data.index),graph_data["PC_D"],label="PC_D")
plt.plot(list(graph_data.index),graph_data["PC_E"],label="PC_E")
# 凡例の表示
plt.legend()
plt.plot(x,y)は折れ線グラフ
plt.plotの解説URL
https://own-search-and-study.xyz/2016/08/08/matplotlib-pyplot%E3%81%AEplot%E3%81%AE%E5%85%A8%E5%BC%95%E6%95%B0%E3%82%92%E4%BD%BF%E3%81%84%E3%81%93%E3%81%AA%E3%81%99/