5

I would like to reduce my code. So instead of 2 lines I would like to select rows by 3 conditions on 2 columns. My DataFrame contains Country's population between 2000 and 2018 by granularity (Total, Female, Male, Urban, Rural)

 Zone Granularity Year Value 0 Afghanistan Total 2000 20779.953 1 Afghanistan Male 2000 10689.508 2 Afghanistan Female 2000 10090.449 3 Afghanistan Rural 2000 15657.474 4 Afghanistan Urban 2000 4436.282 20909 Zimbabwe Total 2018 14438.802 20910 Zimbabwe Male 2018 6879.119 20911 Zimbabwe Female 2018 7559.693 20912 Zimbabwe Rural 2018 11465.748 20913 Zimbabwe Urban 2018 5447.513 

I would like all rows of the Year 2017 with granularity Total AND Urban. I tried something like this below but not working but each condition working well in separate code.

df.loc[(df['Granularity'].isin(['Total', 'Urban'])) & (df['Year'] == '2017')] 

Thanks for tips to help

5
  • For sure df['Year'] == 2017 is working good but also df['Year'] == '2017' and the only way to have result is to use the quote as Int for df.loc[df['Year'] == '2017'] Commented Apr 26, 2022 at 18:26
  • Can you check if, 1. is the year column object type? 2. are there data points where both the conditions are true? Commented Apr 26, 2022 at 18:26
  • please provide the output of df.dtypes in the question Commented Apr 26, 2022 at 18:28
  • Year is an obejct so I need to use '' and yes for all my country I have 2 rows meeting the condition. Actually I got a result in 2 times with 2 dataframe. First df1= df[df['Granularity'].isin(['Total', 'Urban'])].reset_index(drop=True) and then df3= df1[df1['Year'] == '2017'].reset_index(drop=True) Commented Apr 26, 2022 at 18:32
  • then what is the output of df['Year'].unique()? Commented Apr 26, 2022 at 18:33

1 Answer 1

5

Very likely, you're using the wrong type for the year. I imagine these are integers.

You should try:

df.loc[(df['Granularity'].isin(['Total', 'Urban'])) & df['Year'].eq(2017)] 

output (for the Year 2018 as 2017 is missing from the provided data):

 Zone Granularity Year Value 20909 Zimbabwe Total 2018 14438.802 20913 Zimbabwe Urban 2018 5447.513 
Sign up to request clarification or add additional context in comments.

3 Comments

Likewise it is good idea to check for whitespace around strings df['Gradularity'].str.strip().isin([.....
So we can't associate a isin condition with a == operator ?
yes you can, eq and == are equivalent (in most cases), the issue was the type (you can have integers in a object column)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.