Challenge: Multiple Filters
Explore how to apply multiple filters in Pandas dataframes to extract artist names based on country and play count conditions. Understand the use of logical AND and OR operators, and proper syntax with brackets to solve multi-criteria filtering challenges in data analysis.
We'll cover the following...
Problem definition
A music label wants to evaluate the success of its artists in the past month. However, it is unfair to evaluate based on play count across different countries. The music label would like to view at the same time:
- Artists outside the UK who have > 100 plays
- Artists inside the UK who have > 200 plays
Expected outcome
Return the list of artist names who match the filters given above in the problem definition.
Example: ['Pink Floyd', 'The Beatles']
Challenge
Solution
Solution explanation
It is important to note that there are two conditions, where each can be true.
- UK & > 200 plays
- Not UK & > 100 plays
In this case, there should be an OR between the two higher-level conditions, using a pipe |.
However, there are two sub-conditions within each condition, with both needing to be true. Therefore you need an ANDin the form of the & symbol.
When combining multiple conditions in Pandas, each should be encapsulated in a bracket. Therefore the correct method of writing should be as follows:
df[((df['country']=='UK') & (df['plays'] > 200) ) | ((df['country']!='UK') &( df['plays'] > 100))]
The last step is simply getting the artists’ names and returning them as a list.