0

I have two data frames named df1 & df2:

df1

df1

df2

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?

5
  • SITE_NUMBER is the index of df1, not a column. You're referencing it like a column. Commented Oct 17, 2019 at 1:39
  • Yes agreed, is there any other way we could do matching? Commented Oct 17, 2019 at 1:40
  • @vb_rises didn't work. Throw an error message "ValueError: index must be monotonic increasing or decreasing" Commented Oct 17, 2019 at 1:47
  • df1.sort_index().loc[:'SITE_NUMBER'] Commented Oct 17, 2019 at 1:52
  • Better solution is to use pd.merge(df1, df2, left_index=True, right_on='S_STATION') and then extract desired columns. Commented Oct 17, 2019 at 1:56

1 Answer 1

0

You need isin. Use it on df1.index

def return_facility():
   return df1.loc[df1.index.isin(pd.to_numeric(df2['S_STATION'])), 'FACILITY']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.