4
9

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.

R/tidyverseとPython/polars でやることを Julia でやるとどうなるか

Last updated at Posted at 2023-01-27

Julia でやる方法は幾通りかあるが,Query を使う方法を示しておこう。ただし,Query が対応していないものについては,別法で。

using DataFrames, CSV
df = CSV.read("df.csv", DataFrame);
df_long = CSV.read("df_long.csv", DataFrame);
df
3×4 DataFrame
Row date USDJPY EURJPY SPX
Date Float64 Float64 Int64
1 2023-01-01 130.0 140.0 3800
2 2023-01-02 131.0 141.0 3900
3 2023-01-03 130.5 143.5 4000
df_long
9×3 DataFrame
Row date index value
Date String7 Float64
1 2023-01-01 USDJPY 130.0
2 2023-01-01 EURJPY 140.0
3 2023-01-01 SPX 3800.0
4 2023-01-02 USDJPY 131.0
5 2023-01-02 EURJPY 141.0
6 2023-01-02 SPX 3900.0
7 2023-01-03 USDJPY 130.5
8 2023-01-03 EURJPY 143.5
9 2023-01-03 SPX 4000.0
using Query

以下で, df |> ホニャララ |> DataFrame のように最後が |> DataFrame になっているのは,Query の結果が DataFrame ではないためである。DataFrame にしなくても良い場合は,それを省くことができる。また,結果を別のデータフレームにしたい場合は df2 = df |> ホニャララ |> DataFrame にすればよい。ホニャララは何回も書くことができる。

# 列の選択
df |>
    @select(:date, :USDJPY) |>
    DataFrame
3×2 DataFrame
Row date USDJPY
Date Float64
1 2023-01-01 130.0
2 2023-01-02 131.0
3 2023-01-03 130.5
# 列の変更・追加
df |>
    @mutate(USDJPY = 2_.USDJPY) |>
    DataFrame
3×4 DataFrame
Row date USDJPY EURJPY SPX
Date Float64 Float64 Int64
1 2023-01-01 260.0 140.0 3800
2 2023-01-02 262.0 141.0 3900
3 2023-01-03 261.0 143.5 4000
df |>
    @mutate(EURUSD = _.EURJPY / _.USDJPY) |>
    DataFrame
3×5 DataFrame
Row date USDJPY EURJPY SPX EURUSD
Date Float64 Float64 Int64 Float64
1 2023-01-01 130.0 140.0 3800 1.07692
2 2023-01-02 131.0 141.0 3900 1.07634
3 2023-01-03 130.5 143.5 4000 1.09962
# 複数追加する場合は @mutate を繰り返す
df |>
    @mutate(EURUSD = _.EURJPY / _.USDJPY) |>
    @mutate(foo = 100_.EURUSD) |>
    DataFrame
3×6 DataFrame
Row date USDJPY EURJPY SPX EURUSD foo
Date Float64 Float64 Int64 Float64 Float64
1 2023-01-01 130.0 140.0 3800 1.07692 107.692
2 2023-01-02 131.0 141.0 3900 1.07634 107.634
3 2023-01-03 130.5 143.5 4000 1.09962 109.962
# 行の選択
df |>
    @filter(_.date == Date("2023-01-02")) |>
    DataFrame
1×4 DataFrame
Row date USDJPY EURJPY SPX
Date Float64 Float64 Int64
1 2023-01-02 131.0 141.0 3900
# ソート
df |>
    @orderby(_.date) |>
    DataFrame
3×4 DataFrame
Row date USDJPY EURJPY SPX
Date Float64 Float64 Int64
1 2023-01-01 130.0 140.0 3800
2 2023-01-02 131.0 141.0 3900
3 2023-01-03 130.5 143.5 4000
df |>
    @orderby_descending(_.date) |>
    DataFrame
3×4 DataFrame
Row date USDJPY EURJPY SPX
Date Float64 Float64 Int64
1 2023-01-03 130.5 143.5 4000
2 2023-01-02 131.0 141.0 3900
3 2023-01-01 130.0 140.0 3800
# 重複行の削除 全列を対象にする
df_long |>
    @unique() |>
    DataFrame
9×3 DataFrame
Row date index value
Date String7 Float64
1 2023-01-01 USDJPY 130.0
2 2023-01-01 EURJPY 140.0
3 2023-01-01 SPX 3800.0
4 2023-01-02 USDJPY 131.0
5 2023-01-02 EURJPY 141.0
6 2023-01-02 SPX 3900.0
7 2023-01-03 USDJPY 130.5
8 2023-01-03 EURJPY 143.5
9 2023-01-03 SPX 4000.0
# 重複行の削除 特定の列を対象にする
df_long |>
    @unique(_.date) |>
    DataFrame
3×3 DataFrame
Row date index value
Date String7 Float64
1 2023-01-01 USDJPY 130.0
2 2023-01-02 USDJPY 131.0
3 2023-01-03 USDJPY 130.5
# 縦長データに変換
stack(df, [:USDJPY, :EURJPY, :SPX])
9×3 DataFrame
Row date variable value
Date String Float64
1 2023-01-01 USDJPY 130.0
2 2023-01-02 USDJPY 131.0
3 2023-01-03 USDJPY 130.5
4 2023-01-01 EURJPY 140.0
5 2023-01-02 EURJPY 141.0
6 2023-01-03 EURJPY 143.5
7 2023-01-01 SPX 3800.0
8 2023-01-02 SPX 3900.0
9 2023-01-03 SPX 4000.0
# 横長データに変換
unstack(df_long, :index, :value)
3×4 DataFrame
Row date USDJPY EURJPY SPX
Date Float64? Float64? Float64?
1 2023-01-01 130.0 140.0 3800.0
2 2023-01-02 131.0 141.0 3900.0
3 2023-01-03 130.5 143.5 4000.0
# グループ化
gd = groupby(df_long, :index);
using StatsBase
combine(gd, :value => mean => :ave, :value => maximum => :max, :value => minimum => :min)
3×4 DataFrame
Row index ave max min
String7 Float64 Float64 Float64
1 USDJPY 130.5 131.0 130.0
2 EURJPY 141.5 143.5 140.0
3 SPX 3900.0 4000.0 3800.0
4
9
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
4
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?