I have df as follows:
df = pd.DataFrame({"A":[0,np.nan,0,0,np.nan,1,np.nan,1,0,np.nan],
"B":[0,1,0,0,1,1,1,0,0,0]})
Now, I need to replace nan values in column A with values from column B and one above row. for example: 2nd row for column A should be 0, 7th row equals to 1 etc.
I defined this function but it doesnt work trying to apply into dataframe
def impute_with_previous_B(df):
for x in range(len(df)):
if pd.isnull(df.loc[x,"A"]) == True:
df.loc[x,"A"] = df.loc[x-1,"B"]
df["A"] = df.apply(lambda x: impute_with_previous_B(x),axis=1)
Can you please tell me what is wrong with that function ?