我的问题与我为R(divide by previous element by group)发布的问题相同,除了我现在想在Pandas做同样的事情。
我有一个如下所示的数据框:
import gulp from 'gulp';
import riot from 'gulp-riot';
gulp.task('tags', () => {
return gulp.src([`${srcDir}/*`])
.pipe(riot({
type: 'es6'
}))
.pipe(gulp.dest(`${destDir}/`));
});
我想要做的是将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
答案 0 :(得分:0)
使用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