Indi Quant
Indi Quant

Reputation: 28

Updating a df column by another column's minimum value from current row onwards

I have a df that looks like this:

#Output: 
    A     B
0   500   Nan
1   800   Nan
2   700   Nan
3   650   Nan
4   800   Nan
5   780   Nan
6   750   Nan

I want to populate column B with (Minimum A from current row till end).

Desired Result:

#Output: 
    A     B
0   500   500
1   800   650
2   700   650
3   650   650
4   800   750
5   780   750
6   750   750

the following tried method gets the minimum of all A's values, I need to somehow specify the row in there.

df['B'] = df['A'].min()

Any ideas/solutions are appreciated. thank you for you time.

Upvotes: 1

Views: 34

Answers (1)

Mustafa Aydın
Mustafa Aydın

Reputation: 18306

You can take the reversed cumulative minimum for that only-looking ahead behaviour:

df["B"] = df.loc[::-1, "A"].cummin()

to get

>>> df
     A    B
0  500  500
1  800  650
2  700  650
3  650  650
4  800  750
5  780  750
6  750  750

Upvotes: 1

Related Questions