• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

pandas style

date: 2022-09-25 excerpt: pandas styleのチートシート

tag: pythonpandasチートシート


pandas styleのチートシート

概要

  • notebookで可視化する際にスタイルを整えるのに必要
  • builder patternになっている

関数

  • format
    • 桁数や文字のトリム
  • highlight_max
    • 最大値に色を付ける
  • highlight_min
    • 最小値に色を付ける
  • hide
    • インデックスを非表示(1.3.0以降から)
  • background_gradient(cmap='Blues')
    • ヒートマップ

具体例

df.head(10).style.format({"BasePay": "${:20,.0f}", 
                          "OtherPay": "${:20,.0f}", 
                          "TotalPay": "${:20,.0f}",
                          "TotalPayBenefits":"${:20,.0f}"}) \
                 .format({"JobTitle": lambda x:x.lower(), "EmployeeName": lambda x:x.lower()}) \
                 .hide() \ # 1.3.0からhide関数になった
                 .background_gradient(cmap='Blues')

参考

  • Style Pandas Dataframe Like a Master


pythonpandasチートシート Share Tweet