It's logically exactly the same. Don't know rationale for replicating Excel type logic into pandas
import numpy as np
df = pd.DataFrame({"A":["","London","Singapore","London","KL","New York"],"B":["London","","","","",""],"C":["","","","","",""],"D":[25,10,"","","",""],"E":["","","","","",""],"F":["","","","","",""],"G":["","","","","",""]})
print(f"""{df.to_string()}
np.where((df.loc[1:5,'A']==df.loc[0,'B']).sum()>0,(df.loc[0,"D"]/df.loc[1,"D"]),0)
{np.where((df.loc[1:5,'A']==df.loc[0,'B']).sum()>0,(df.loc[0,"D"]/df.loc[1,"D"]),0)}
np.where((df.loc[1:5,'A']==df.loc[0,'C']).sum()>0,(df.loc[0,"D"]/df.loc[1,"D"]),0)
{np.where((df.loc[1:5,'A']==df.loc[0,'C']).sum()>0,(df.loc[0,"D"]/df.loc[1,"D"]),0)}
""")
output
A B C D E F G
0 London 25
1 London 10
2 Singapore
3 London
4 KL
5 New York
np.where((df.loc[1:5,'A']==df.loc[0,'B']).sum()>0,(df.loc[0,"D"]/df.loc[1,"D"]),0)
2.5
np.where((df.loc[1:5,'A']==df.loc[0,'C']).sum()>0,(df.loc[0,"D"]/df.loc[1,"D"]),0)
0.0