How to efficiently register row change rate in a Pandas DataFrame?

Let's say I have a DataFrame (in my case about 10,000 rows, this is just a minimal example)

>>> import pandas as pd

>>> sample_df = pd.DataFrame(
        {'col1': list(range(1, 10)), 'col2': list(range(10, 19))})

>>> sample_df

   col1  col2
0     1    10
1     2    11
2     3    12
3     4    13
4     5    14
5     6    15
6     7    16
7     8    17
8     9    18

For my purposes, I need to calculate the series presented ln(col_i(n+1) / col_i(n))for each col_iin my DataFrame, where it nrepresents the row number. How can I calculate this ?


Background knowledge

I know that I can get the difference between each column in a very simple way using

>>> sample_df.diff()

   col1  col2
0   NaN   NaN
1     1     1
2     1     1
3     1     1
4     1     1
5     1     1
6     1     1
7     1     1
8     1     1

Or the percentage change that (col_i(n+1) - col_i(n))/col_i(n+1)using

>>> sample_df.pct_change()

       col1      col2
0       NaN       NaN
1  1.000000  0.100000
2  0.500000  0.090909
3  0.333333  0.083333
4  0.250000  0.076923
5  0.200000  0.071429
6  0.166667  0.066667
7  0.142857  0.062500
8  0.125000  0.058824

I just struggled with a simple way to get the direct division of each successive column into the previous one. If I knew how to do this, I could simply apply the natural logarithm to each element of the series after the fact.

, 1 , . .

!

+4
3

IIUC:

log - logs:

sample_df.apply(np.log).diff()

:

np.log(sample_df).diff()

enter image description here


Timing

enter image description here

+3

np.log:

np.log(df.col1 / df.col1.shift())

, @nikita, .

, , :

np.log(df / df.shift())
+4

shift, , .

>>> sample_df['col1'].shift()
0    NaN
1    1.0
2    2.0
3    3.0
4    4.0
5    5.0
6    6.0
7    7.0
8    8.0
Name: col1, dtype: float64

:

import math
(sample_df['col1'] / sample_df['col1'].shift()).apply(lambda row: math.log(row))

0         NaN
1    0.693147
2    0.405465
3    0.287682
4    0.223144
5    0.182322
6    0.154151
7    0.133531
8    0.117783
Name: col1, dtype: float64
+1

Source: https://habr.com/ru/post/1648288/


All Articles