LoginSignup
0
0

More than 1 year has passed since last update.

Pivot table

Posted at

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

Suppliers.png

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)

Suppliers.png


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0