Search⌘ K
AI Features

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.

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:

  1. Artists outside the UK who have > 100 plays
  2. 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

Python
import pandas as pd
def test():
df = pd.read_csv('music.csv')
pass

Solution

Python
import pandas as pd
def test():
df = pd.read_csv('music.csv')
out = df[((df['country']=='UK') & (df['plays'] > 200) ) | ((df['country']!='UK') &( df['plays'] > 100))]
return list(out['artist'].values)
print(test())

Solution explanation

It is important to note that there are two conditions, where each can be true.

  1. UK & > 200 plays
  2. 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.