MCM
MCM

Reputation: 1511

Proper IF Statement python

Hi I wanted to select certain data from my dataframe as shown below. Somehow my If statement does not work properly and selects, e.g. instead of 6M and 6M3M all of the possibilities which means 6M, 3M and 6M3M. I would like to play with the if statement. The code looks like the following:

import pandas as pd

df = pd.DataFrame([
        ['Alex', 'Germany', '6M', 28, 'Surfer'],        
        ['Paul', 'Germany', '6M', 25,  'Surfer'],        
        ['Carly', 'Germany', '6M', 22,  'Surfer'],          
        ['Richard', 'Germany', '6M', 20,  'Surfer'],          
        ['Lee', 'UK', '6M3M', 22,  'Kite'],       
        ['Bob', 'UK', '6M3M', 23,  'Kite'],       
        ['Judith', 'UK', '6M3M', 29,  'Kite'],       
        ['Stephan', 'Spain', '3M', 29,  'Basketball'],       
        ['Wendy', 'Spain', '3M', 23,  'Basketball'],       
        ['Dennis', 'Spain', '3M', 25,  'Basketball'],
        ['Tucker', 'Germany', '3M', 22,  'Basketball'],        
        ['Paula', 'Germany', '3M', 21,  'Basketball'],          

        ], columns=['Name', 'Country', 'Course', 'Age', 'Sport'])

for index, row in df.iterrows():
        if (row['Country'] == 'Germany') or (row['Country'] == 'UK') or (row['Course'] == '6M') or (row['Course'] == '6M3M'):
        print(row['Name'], row['Country'], row['Age'], row['Sport'], row['Course'])  

My Output looks like:

Alex Germany 28 Surfer 6M
Paul Germany 25 Surfer 6M
Carly Germany 22 Surfer 6M
Richard Germany 20 Surfer 6M
Lee UK 22 Kite 6M3M
Bob UK 23 Kite 6M3M
Judith UK 29 Kite 6M3M
Tucker Germany 22 Basketball 3M
Paula Germany 21 Basketball 3M  

But I wanted to have only:

Alex Germany 28 Surfer 6M
Paul Germany 25 Surfer 6M
Carly Germany 22 Surfer 6M
Richard Germany 20 Surfer 6M
Lee UK 22 Kite 6M3M
Bob UK 23 Kite 6M3M
Judith UK 29 Kite 6M3M

Upvotes: 0

Views: 89

Answers (1)

Farhan.K
Farhan.K

Reputation: 3485

Your if logic is not correct. It should be:

if row['Country'] in {'Germany', 'UK'} and row['Course'] in {'6M', '6M3M'}:
        print(row['Name'], row['Country'], row['Age'], row['Sport'], row['Course'])

Have a look at this answer for an explanation on using in in an if statement.

Upvotes: 2

Related Questions