8

I have a database that I am bringing in a SQL table of events and alarms (df1), and I have a txt file of alarm codes and properties (df2) to watch for. Want to use 1 columns values from df2 that each value needs cross checked against an entire column values in df1, and output the entire rows of any that match into another dataframe df3.

df1 A B C D 0 100 20 1 1 1 101 30 1 1 2 102 21 2 3 3 103 15 2 3 4 104 40 2 3 df2 0 1 2 3 4 0 21 2 2 3 3 1 40 0 NaN NaN NaN 

Output entire rows from df1 that column B match with any of df2 column 0 values into df3.

df3 A B C D 0 102 21 2 3 1 104 40 2 3 

I was able to get single results using:

df1[df1['B'] == df2.iloc[0,0]] 

But I need something that will do this on a larger scale.

1 Answer 1

17

Method 1: merge

Use merge, on B and 0. Then select only the df1 columns

df1.merge(df2, left_on='B', right_on='0')[df1.columns] A B C D 0 102 21 2 3 1 104 40 2 3 

Method 2: loc

Alternatively use loc to find rows in df1 where B has a match in df2 column 0 using .isin:

df1.loc[df1.B.isin(df2['0'])] A B C D 2 102 21 2 3 4 104 40 2 3 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.