Aksel Etingu
Aksel Etingu

Reputation: 195

Back Filling Dataframe

I have a dataframe with 3 columns. Something like this:

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

I would like to populate the prior rows with the Initial Amount as such:

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

So find the:

Regards,

Upvotes: 0

Views: 29

Answers (1)

sjw
sjw

Reputation: 6543

Pandas fillna with fill method 'bfill' (uses next valid observation to fill gap) should do what you're looking for:

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

Upvotes: 1

Related Questions