Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

col

livebooks/explorer/col.livemd

col

Mix.install([
  {:explorer, "~> 0.8"},
  {:kino, "~> 0.12"}
])

準備

alias Explorer.DataFrame
alias Explorer.Series
import Explorer.DataFrame

通常の場合

df =
  DataFrame.new(%{
    "x1" => 1..5,
    "x2" => 2..10//2,
    "x3" => 3..15//3
  })

Kino.DataTable.new(df)
df
|> DataFrame.filter(x1 > 3)
|> Kino.DataTable.new()
df
|> DataFrame.mutate(x4: x2 * 2)
|> Kino.DataTable.new()

列名をそのまま使えない場合

df =
  DataFrame.new(%{
    "1x" => 1..5,
    "2x" => 2..10//2,
    "3x" => 3..15//3
  })

Kino.DataTable.new(df)
df
|> DataFrame.filter(col("1x") > 3)
|> Kino.DataTable.new()
df
|> DataFrame.mutate([{"4x", col("2x") * 2}])
|> Kino.DataTable.new()

列名に変数を使う場合

1..3
|> Enum.reduce(df, fn index, acc_df ->
  DataFrame.mutate(acc_df, [{^"-#{index}x", col(^"#{index}x") * -1}])
end)
|> Kino.DataTable.new()