Rohan
Rohan

Reputation: 47

Compare two dataframe and match the the values

I have two dataframes df1

Questions Answers
Was Abraham Lincoln the sixteenth President of the United States? Yes
Did Lincoln sign the National Banking Act of 1863? yes
Did his mother die of pneumonia? no
How many long was Lincoln's formal education? 18 months
When did Lincoln begin his political career? 1832
What did The Legal Tender Act of 1862 establish? the United States Note, the first paper currency in United States history
Who suggested Lincoln grow a beard? 11-year-old Grace Bedell
When did the Gettysburg address argue that America was born? 1776
Did Lincoln beat John C. Breckinridge in the 1860 election? yes
•Was Abraham Lincoln the first President of the United States? No
Did Lincoln start his political career in 1832? Yes
Did Lincoln ever represent Alton & Sangamon Railroad? Yes
Abraham_Lincoln Which county was Lincoln born in? Hardin County
When did Lincoln first serve as President? March 4, 1861
Who assassinated Lincoln? John Wilkes Booth
Did Lincoln win the election of 1860? Yes

I have another dataframe df2

Questions Answers
Was Abraham Lincoln the sixteenth President of the United States? No
Did Lincoln sign the National Banking Act of 1863? yes
Did his mother die of pneumonia? yes
How many long was Lincoln's formal education? 18 months
When did Lincoln begin his political career? 1832
What did The Legal Tender Act of 1862 establish? the United States Note, the first paper currency in United States history
Who suggested Lincoln grow a beard? 11-year-old Grace Bedell
When did the Gettysburg address argue that America was born? 1776
Did Lincoln beat John C. Breckinridge in the 1860 election? yes
•Was Abraham Lincoln the first President of the United States? No
Did Lincoln start his political career in 1832? Yes
Did Lincoln ever represent Alton & Sangamon Railroad? No
Which county was Lincoln born in? Hardin County
When did Lincoln first serve as President? March 4, 1861
Who assassinated Lincoln? John Wilkes Booth
Did Lincoln win the election of 1860? No

I need to compare the first dataframe with the second and if the question and answer match then give output true else false create a new dataframe in the following format

Result

Question Answer is_match
Ques1 answer1 false
Ques2 answer 2 true
Ques3 answer3 false

Upvotes: 1

Views: 42

Answers (1)

jezrael
jezrael

Reputation: 862521

Use DataFrame.merge with left or outer join and indicator parameter, for boolean compare by both:

df = df1.merge(df2, how='left', indicator='is_match')
df['is_match'] = df['is_match'].eq('both')

Upvotes: 1

Related Questions