Al_Iskander Al_Iskander - 5 months ago 15
Python Question

filling forward conditional result

I have a DataFrame with columns

A
and
B
. Now I want to produce column
C
like this:

A B C
index
1 0 50 NaN
2 1 60 60
3 0 40 60
4 0 30 60
5 1 40 40


C
gets the value of
B
if
A==1
in this row. Then this value is kept in coming rows until next time
A==1
.How can I do this in vectorized fashion?

Answer

You can select the values of B where A==1, then fill forward:

a = pd.DataFrame({"A":[0,1,0,0,1], "B":[50,60,40,30,40]}, index=[1,2,3,4,5])
a["C"] = a.B[a.A == 1]
a = a.fillna(method="ffill")

The ffill method propagates forward the last valid observation to fill in the NaNs. See http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.fillna.html for more info.

This gives:

   A   B   C
1  0  50 NaN
2  1  60  60
3  0  40  60
4  0  30  60
5  1  40  40
Comments