Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 回填数据帧_Python - Fatal编程技术网

Python 回填数据帧

Python 回填数据帧,python,Python,我有一个3列的数据框。大概是这样的: Data Initial_Amount Current 31-01-2018 28-02-2018 31-03-2018 30-04-2018 100 100 31-05-2018 100 90 30-06-2018 100 80 我想用初始金额填充前面的行,如下所示: Data Initial_Amount Current 31-0

我有一个3列的数据框。大概是这样的:

Data        Initial_Amount   Current
31-01-2018
28-02-2018
31-03-2018
30-04-2018  100              100
31-05-2018  100               90
30-06-2018  100               80
我想用初始金额填充前面的行,如下所示:

Data        Initial_Amount   Current
31-01-2018  100              100
28-02-2018  100              100 
31-03-2018  100              100
30-04-2018  100              100
31-05-2018  100               90
30-06-2018  100               80
因此,请找到:

  • 填充初始金额的第一个非_空行
  • 使用该选项将初始金额回填到起始日期
  • 如果是第一行且当前为空,则复制初始金额,否则复制先前余额
考虑到,

具有填充方法的熊猫
'bfill'
(使用下一个有效的观察值来填充间隙)应该做您想要做的事情:

In [13]: df.fillna(method='bfill')

Out[13]: 
         Data  Initial_Amount  Current
0  31-01-2018           100.0    100.0
1  28-02-2018           100.0    100.0
2  31-03-2018           100.0    100.0
3  30-04-2018           100.0    100.0
4  31-05-2018           100.0     90.0
5  30-06-2018           100.0     80.0

请张贴您试图解决问题的代码以显示您的努力。