Это решение использует цикл for, но он зацикливается на значениях A, где он равен NaN.
A = The column containing NaNs
B = The column to be referenced
import pandas as pd
import numpy as np
#Consider this dataframe
df = pd.DataFrame({'A':[1,2,3,4,np.nan,6,7,8,np.nan,10],'B':['xxxx','b','xxxx','d','xxxx','f','yyyy','h','yyyy','j']})
A B
0 1.0 xxxx
1 2.0 b
2 3.0 xxxx
3 4.0 d
4 NaN xxxx
5 6.0 f
6 7.0 yyyy
7 8.0 h
8 NaN yyyy
9 10.0 j
for i in list(df.loc[np.isnan(df.A)].index): #looping over indexes where A in NaN
#dict with the keys as B and values as A
#here the dict keys will be unique and latest entries of B, hence having latest corresponding A values
dictionary = df.iloc[:i+1].dropna().set_index('B').to_dict()['A']
df.iloc[i,0] = dictionary[df.iloc[i,1]] #using the dict to change the value of A
Так выглядит df после выполнения кода
A B
0 1.0 xxxx
1 2.0 b
2 3.0 xxxx
3 4.0 d
4 3.0 xxxx
5 6.0 f
6 7.0 yyyy
7 8.0 h
8 7.0 yyyy
9 10.0 j
Обратите внимание, что при index = 4 значения A изменяются на 3.0, а не 1.0