【问题标题】:How to get log rate of change between rows in Pandas DataFrame effectively?如何有效地获取 Pandas DataFrame 中行之间的日志变化率?
【发布时间】:2016-11-21 12:49:11
【问题描述】:

假设我有一些 DataFrame(在我的例子中大约有 10000 行,这只是一个最小的例子

>>> 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

出于我的目的,我需要为我的 DataFrame 中的每个col_i 计算由ln(col_i(n+1) / col_i(n)) 表示的系列,其中n 表示一个行号。 如何计算


背景知识

我知道我可以使用

以非常简单的方式获得每列之间的差异
>>> 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

或百分比变化,即(col_i(n+1) - col_i(n))/col_i(n+1),使用

>>> 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

我一直在努力寻找一种直接将每个连续列直接除以前一个列的方法。如果我知道如何做到这一点,我可以在事后将自然对数应用于系列中的每个元素。

目前为了解决我的问题,我正在求助于创建另一列,每列将行元素向下移动 1,然后在两列之间应用公式。不过,这对我来说似乎很混乱且次优。

任何帮助将不胜感激!

【问题讨论】:

    标签: python numpy pandas dataframe series


    【解决方案1】:

    只需使用 np.log:

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

    您也可以按照@nikita 的建议使用 apply ,但这会更慢。

    此外,如果您想对整个数据框执行此操作,您可以这样做:

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

    【讨论】:

    • 不错的补充,但我想“转变”是这里的关键。
    【解决方案2】:

    IIUC:

    一个比率的对数是对数的差:

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

    或者更好:

    np.log(sample_df).diff()
    


    时间

    【讨论】:

    • 我的数学疏忽。谢谢!
    【解决方案3】:

    您可以为此使用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
    

    【讨论】:

    • Gahh 我知道必须有一个简单的功能我为此缺少。绝对是一个进步。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 2021-06-16
    • 2018-03-30
    • 2012-10-05
    • 2019-08-17
    • 2018-07-27
    相关资源
    最近更新 更多