Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you apply a shift to a Julia Dataframe?

Tags:

In python pandas, the shift function is useful to shift the rows in the dataframe forward and possible relative to the original which allows for calculating changes in time series data. What is the equivalent method in Julia?

like image 393
ndw Avatar asked Oct 27 '20 20:10

ndw


People also ask

How does shift work in pandas?

shift() function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called the period, which represents the number of shifts to be made over the desired axis. This function is very helpful when dealing with time-series data.

How do I select columns in Julia?

We can use: Symbol : select(df, :col) String : select(df, "col") Integer : select(df, 1)

How do I change the column name in Julia DataFrame?

Additionally, in your example, you should use select! in order to modify the column names in place, or alternatively do 'df = select(df, "col1" => "Id", "col2" => "Name")` as select always return a new DataFrame .


2 Answers

Normally one would use ShiftedArrays.jl and apply it to columns that require shifting.

like image 109
Bogumił Kamiński Avatar answered Sep 24 '22 00:09

Bogumił Kamiński


Here is a small working example:

using DataFrames, ShiftedArrays

df = DataFrame(a=1:3, b=4:6)
3×2 DataFrame
 Row │ a      b     
     │ Int64  Int64 
─────┼──────────────
   1 │     1      4
   2 │     2      5
   3 │     3      6

transform(df, :a => lag => :lag_a)
3×3 DataFrame
 Row │ a      b      lag_a   
     │ Int64  Int64  Int64?  
─────┼───────────────────────
   1 │     1      4  missing 
   2 │     2      5        1
   3 │     3      6        2

or you could do:

df.c = lag(df.a)

or, to have the lead of two rows:

df.c = lead(df.a, 2)

etc.

like image 31
Timothée HENRY Avatar answered Sep 24 '22 00:09

Timothée HENRY