creating Pivot table.
# value: valueを指定しないとカラムに選択されていない全てのデータでテーブルが作成されてしまう。
# aggfunc:集計方法
# margins_name=で名称変更
# fill_value=0で欠損値を0で埋める
# デフォルトではdropna=Trueとなっており、全ての行列が欠損値の場合は表示されない。表示するにはdropna=False
df_pivot = pd.pivot_table(df,values = "Claim Qty", index = "customer_ENG", columns = "Suppliers",
aggfunc = ['count'], margins=True, margins_name="total",
fill_value=0, dropna=True)
df_pivot
sorting the pivot: get index of the sorted values and reindex the initial pivot table.
df_pivot2 = df_pivot.reindex(df_pivot["count"].sort_values(by= 'total', ascending=False).index)
df_pivot2.head(10)