用前面的元素除以大Pandas中的组

2024-04-25 04:12:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题与我为R(divide by previous element by group)发布的问题相同,只是我现在想在Pandas中做同样的事情。你知道吗

我有一个如下创建的数据框:

origdate = pd.Series(np.repeat(['2011-01-01', '2011-02-01', '2011-03-01'],[5, 4, 3]))
date = pd.Series(['2011-01-01', '2011-02-01', '2011-03-01', '2011-04-01', '2011-05-01', 
'2011-02-01', '2011-03-01', '2011-04-01', '2011-05-01', '2011-03-01', '2011-04-01', '2011-05-01'])
bal = pd.Series(range(20,32))
A = pd.DataFrame({'origdate': origdate, 'date': date, 'bal': bal})
A
        bal date    origdate
    0   20  2011-01-01  2011-01-01
    1   21  2011-02-01  2011-01-01
    2   22  2011-03-01  2011-01-01
    3   23  2011-04-01  2011-01-01
    4   24  2011-05-01  2011-01-01
    5   25  2011-02-01  2011-02-01
    6   26  2011-03-01  2011-02-01
    7   27  2011-04-01  2011-02-01
    8   28  2011-05-01  2011-02-01
    9   29  2011-03-01  2011-03-01
    10  30  2011-04-01  2011-03-01
    11  31  2011-05-01  2011-03-01

我要做的是用bal除以前一个bal得到date的每一个增量,而不是当origdate改变时。所以我想得到的是在dbal列中显示的:

     origdate       date bal     dbal
1  2011-01-01 2011-01-01  20       NA
2  2011-01-01 2011-02-01  21 1.050000
3  2011-01-01 2011-03-01  22 1.047619
4  2011-01-01 2011-04-01  23 1.045455
5  2011-01-01 2011-05-01  24 1.043478
6  2011-02-01 2011-02-01  25       NA
7  2011-02-01 2011-03-01  26 1.040000
8  2011-02-01 2011-04-01  27 1.038462
9  2011-02-01 2011-05-01  28 1.037037
10 2011-03-01 2011-03-01  29       NA
11 2011-03-01 2011-04-01  30 1.034483
12 2011-03-01 2011-05-01  31 1.033333

Tags: 数据pandasdatebygroupelement事情series
1条回答
网友
1楼 · 发布于 2024-04-25 04:12:47

使用groupby+pct_change+add(1)

A.assign(dbal=A.groupby('origdate').bal.pct_change().add(1))

groupby+shift+div

A.assign(dbal=A.groupby('origdate').bal.apply(lambda x: x.div(x.shift())))

两者都屈服

    bal        date    origdate      dbal
0    20  2011-01-01  2011-01-01       NaN
1    21  2011-02-01  2011-01-01  1.050000
2    22  2011-03-01  2011-01-01  1.047619
3    23  2011-04-01  2011-01-01  1.045455
4    24  2011-05-01  2011-01-01  1.043478
5    25  2011-02-01  2011-02-01       NaN
6    26  2011-03-01  2011-02-01  1.040000
7    27  2011-04-01  2011-02-01  1.038462
8    28  2011-05-01  2011-02-01  1.037037
9    29  2011-03-01  2011-03-01       NaN
10   30  2011-04-01  2011-03-01  1.034483
11   31  2011-05-01  2011-03-01  1.033333

相关问题 更多 >