I have this excel formula that needs to be applied on a pandas dataframe
=IF(OFFSET(list!$B$1,MATCH($X1,list!$A:$A,0)-1,0)=0,"",OFFSET(list!$B$1,MATCH($X1,list!$A:$A,0)-1,0))
dataframe: list
A B C
24309 Pepsi US
45768 McDonalds US
45638 Apple Ireland
59374 Google Ireland
dataframe 2: new_list
X Y
24309 ?
45768 ?
45638
59374
The goal is to populate the Y
column of the second new_list
dataframe if the value in X
matches a value in range A1:A4
I'm struggling with the best approach to do this. Can someone point me to the right direction? I was thinking of using panda groupby
with np.where
to compare and take the value of B
column when there is a match.
Edit
new_list['Y'] = np.where(list['A'] == new_list['X'], list['B'], np.nan)
The solution above gives the same results but is this the right way to define such an Excel formula in Pandas?