0

I am trying to execute a script under 2 conditions, If both the conditions are satisfied then only further code can be executed.

Input Data: Sheet1

Name        Contact_number  id_number    accuracy
Eric        9786543628      AZ256hy         90
Jack        9786543628      AZ98kds         85
Tom         9784533930      AZ256hc         80
Alan        9778934593      AZ256py         70

Sheet2

Name        Contact_number  id_number 
Eric        9786543628      AZ256hy  
Jack        9786543628      AZ98kds   
Tom         9784533930      AZ256hc   
David       9778984893      JH657lv 

Expected Output:

Name        Contact_number  id_number 
    Eric        9786543628      AZ256hy  
    Jack        9786543628      AZ98kds  

Code i have been using :

df = pd.read_excel(open(r'input.xlsx', 'rb'), sheet_name='sheet1')
df2 = pd.read_excel(open(r'input.xlsx', 'rb'), sheet_name='sheet2')

if df['id_number'] = df2['id_number']  and df['accuracy'] =>85:
   df['Name'][index] = df2['Name']
   df['Contact_number'][index] = df2['Contact_number']

But while using this condition the if statement is not working with AND condition. Any suggestion ?

6
  • Shouldn't it be df1["accuracy"]? Commented Apr 8, 2021 at 18:44
  • You're not looping over the rows in the dataframes. Commented Apr 8, 2021 at 18:46
  • Please post your expected output. Commented Apr 8, 2021 at 18:57
  • @Barmar - Yes Typing error, Corrected it. How looping over the rows in the dataframes can be done ? Commented Apr 8, 2021 at 18:57
  • @MayankPorwal - Question has been updated with expected output Commented Apr 8, 2021 at 19:05

2 Answers 2

3

Edit: I made changes to the conditions. This should work

df = pd.read_excel(open(r'input.xlsx', 'rb'), sheet_name='sheet1')
df2 = pd.read_excel(open(r'input.xlsx', 'rb'), sheet_name='sheet2')

df.loc[(df['id_number'] == df2['id_number']) & (df['accuracy']>= 85),['Name','Contact_number', 'id_number']]
Sign up to request clarification or add additional context in comments.

2 Comments

I suspect this is a copying error, since that wouldn't work without the and condition and they'd get a syntax error in either case.
I don't think this is the right way to compare two series.
2

You can use Series.isin:

In [1998]: res = df1[df1.id_number.isin(df2.id_number) & df1.accuracy.ge(85)]

In [1999]: res
Out[1999]: 
   Name  Contact_number id_number  accuracy
0  Eric      9786543628   AZ256hy        90
1  Jack      9786543628   AZ98kds        85

EDIT: If you want only certain columns:

In [2089]: res = df1[df1.id_number.isin(df2.id_number) & df1.accuracy.ge(85)][['Name', 'Contact_number']]

In [2090]: res
Out[2090]: 
   Name  Contact_number
0  Eric      9786543628
1  Jack      9786543628

3 Comments

what would be the case if i need to extract only two columns [''Name", "Contact_number"] values after satisfying the condition
Do res[['Name', 'Contact_number']]
Please suggest how to get only 2 specified columns. how we can get it.

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.