pandas

Python

panderaでUnion型を扱う方法

pandera (0.18.0) ではUnion型がサポートされていなさげ。そのためちょっとだけ自分でコードを書く必要がある。例えばあるカラムが文字列or数値であることを検証するには、以下のようにすればよい。from datetime im...
Python

DataFrameのmapとかapplyとか

DataFrameのmapとかapplyとか、いろいろあって間違えがちなので、メモimport pandas as pdimport polars as pldf = (適当に作成)# pandas の場合df['a'].map(lambd...
Python

DataFrameの手動ソートのトリック

df.col = df.col.astype("category")df.col.cat.set_categories(sorted_list, inplace=True)df = df.sort_values(["col"]).reset...
Python

pythonでExcel作成

# 桁区切りのカンマありの整数format_int = {'num_format': '#,##0', 'align': 'right'}# 小数点以下2桁format_flt = {'num_format': '#,##0.00', 'a...
Python

DataFrameとCOALESCE

# df1(id, col) # df2(id, col)やりたきこと:df1をdf2で更新-- SQLSELECTCOALESCE(b.id, a.id) AS id, COALESCE(b.col, a.col) AS colFROM ...