按行划分

时间:2018-03-26 03:04:51

标签: python pandas numpy

我有一个看起来像这样的数据框

Date    |index_numer
26/08/17|200
27/08/17|300
28/08/17|400
29/08/17|100
30/08/17|150
01/09/17|160
02/09/17|170
03/09/17|280

我正在尝试进行除法,其中第一行除以第二行。

Date    |index_numer| Divison by next row
26/08/17|200        | 0.666
27/08/17|300        | 0.75
28/08/17|400        | 4
29/08/17|100        |..

我在for循环中执行了此操作,然后提取了分区编号并合并了DF。但是,我不确定它是否可以在pandas / numpy中完成。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:3)

使用shift

df['divison'] = df['index_numer'] / df['index_numer'].shift(-1)

输出:

   Date      index_numer   divison
0  26/08/17          200  0.666667
1  27/08/17          300  0.750000
2  28/08/17          400  4.000000
3  29/08/17          100  0.666667
4  30/08/17          150  0.937500
5  01/09/17          160  0.941176
6  02/09/17          170  0.607143
7  03/09/17          280       NaN
相关问题