I have two data frames named df1 & df2:
df1
df2
SITE_NUMBER is set as the index of df1. Now I want to return the 'FACILITY' column of df1 if the values of 'S_STATION' of df2 matches with the index value of df1. I wrote a function for this purpose:
def return_facility():
return df1[df1['SITE_NUMBER'] == pd.to_numeric(df2['S_STATION'])]['FACILITY']
However, the function is not working because compiler does not recognize df1['SITE_NUMBER']. Could anyone point out where I am making mistake?


SITE_NUMBERis the index of df1, not a column. You're referencing it like a column.df1.sort_index().loc[:'SITE_NUMBER']pd.merge(df1, df2, left_index=True, right_on='S_STATION')and then extract desired columns.