Selecting Rows with Score in a Range

Write a Pandas program to select the rows the score is between 15 and 20 (inclusive).

Example 1:

Input: DataFrame with 'score' column 
Output: Rows with 'score' between 15 and 20

Example 2:

Input: DataFrame with 'score' column 
Output: Rows with 'score' between 15 and 20

Use pandas DataFrame conditional selection with logical operators.

import pandas as pd

def select_score_range(df):
    return df[(df['score'] >= 15) & (df['score'] <= 20)]

df1 = pd.DataFrame({'name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'score': [18, 19, 20, 21]})
df2 = pd.DataFrame({'name': ['Adam', 'Eve', 'Stark', 'Rogers'], 'score': [14, 15, 16, 17]})

print(select_score_range(df1))
print(select_score_range(df2))

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!