2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[自分用]polars移行メモ

Posted at

概要

polarsが早いと噂なので(遅すぎるが)移行したいので自分用メモ。

参考

環境

  • google colaboratory

sample

データ入出力

csv読み込み

import polars as pl

df = pl.read_csv("iris.csv")
  • csv読み込みはpandasと同様
  • オプションのキーワードが違うので注意

csv出力

df.write_csv("iris2.csv")
  • csv出力はwrite_csvを使用する
    • pandasの場合はto_csv

データ可視化

先頭、末尾

df.head()
df.tail()
  • デフォルトで5行出力

サマリー

df.describe()

""" 出力サンプル
describe	sepal_length	sepal_width	petal_length	petal_width	species
str	f64	f64	f64	f64	str
"count"	150.0	150.0	150.0	150.0	"150"
"null_count"	0.0	0.0	0.0	0.0	"0"
"mean"	5.843333	3.054	3.758667	1.198667	null
"std"	0.828066	0.433594	1.76442	0.763161	null
"min"	4.3	2.0	1.0	0.1	"setosa"
"max"	7.9	4.4	6.9	2.5	"virginica"
"median"	5.8	3.0	4.35	1.3	null
"25%"	5.1	2.8	1.6	0.3	null
"75%"	6.4	3.3	5.1	1.8	null
"""
  • describeも同様に使用できる
    • null_countが見えるのが偉い

データフレームに対する処理

フィルタリング処理(pandasと使い勝手が大きく違うので注意)

例: irisデータセットからsepal_lengthが7cm以上の行を抽出する

df.filter(7 <= pl.col("sepal_length"))

列の追加(pandasと使い勝手が大きく違うので注意)

例: irisデータセットからsepal_lengthのサイズに応じて["S", "M", "L"]のラベルを新たな列を作成し付与する

# applyを使用する場合
df.with_columns(
    df["sepal_length"].apply(lambda x: "S" if x < 5 else ("M" if x < 7 else "L")).alias("SML")
)
# EXPRESSIONを使う場合
df.with_columns(
    pl.when(pl.col("sepal_length") < 5).then("S")
    .when(pl.col("sepal_length") < 7).then("M")
    .otherwise("L")
    .alias("SML")
)
  • 列の追加にはwith_columnsを使用する
    • applyはpandas同様に使用できる
    • expressionを使うことも可能
  • 追加する列のcolumn名はaliasで指定する

データフレームのソート

例:sepal_lengthで降順にソート後sepal_widthで昇順にソートする

df.sort(["sepal_length", "sepal_width"], descending=[True, False])
  • pandasのsort_valuesとほぼ同じ
    • オプションの向きが違うので注意(pandasはascending)

遅延評価(pandasと使い勝手が大きく違うので注意)

# csv読み出しの時から遅延評価する
df = (
    pl.scan_csv("iris.csv")
    .filter(5 <= pl.col("sepal_length"))
    .with_columns((pl.col("sepal_length") / pl.col("sepal_width")).alias("sepal_ratio"))
    .groupby("species").agg(
        [
            pl.col("sepal_length", "sepal_width", "sepal_ratio").mean().suffix("_mean")
        ]
    )
    .collect()
)

# 特定の処理を遅延評価する
df = (
    df
    .lazy()
    .filter(5 <= pl.col("sepal_length"))
    .with_columns((pl.col("sepal_length") / pl.col("sepal_width")).alias("sepal_ratio"))
    .groupby("species").agg(
        [
            pl.col("sepal_length", "sepal_width", "sepal_ratio").mean().suffix("_mean")
        ]
    )
    .collect()
)
  • pandasにない概念
  • scan_csv()~collect()で挟んだ箇所はpolarsが最適化してまとめて実行する
  • 公式は遅延評価を推奨している
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?