So I have two different dataframes df1(len 16) and df2(len 55) and I want to bring a value from df2 but only if certain conditions are met, 3 to be exactly. I´ve tried np.where() and pd.merge() but I can´t get the value that I want. My dataframes are:
df1 = pd.DataFrame({'Data1':['X','X','X','X','Y','Y','Y','Y','Z','Z','Z','Z'],'Data2':[0,10,20,30,40,50,60,70,80,90,100,110]})
df2 = pd.DataFrame({'Data1':['X','X','X','X','Y','Y','Y','Y','Z','Z','Z','Z',...],'Data2':[0,11,21,31,41,51,61,71,81,91,101,111,...],
'Data3':[10,20,30,40,50,60,70,80,90,100,110,...],'Data4':['A','B','C','D','A','B','C','D','A','B','C','D',...])
I want to make a multiple comparison, df1[Data1] == df2[Data1] & (df1[Data2] >= df2[Data2] & df1[Data2] <= df2[Data3]). If row value of Data1 in df1 equals row value of Data1 in df2 and row value of Data 2 in df1 is between row values of Data2 and Data3 in df2 bring me the row value of Data4 in df2. I´ve tried this:
df1[Data3] = np.where((df1[Data1] == df2[Data1]) & (df1[Data2] >= df2[Data2] & df1[Data2] <= df2[Data3]),df2[Data4],"NA")
But I get ValueError: Can only compare identically-labeled Series objects, because of the difference in length of my series.
I want to avoid using foor loops with pandas, but I don´t see other way of doing it, any suggestions?