Chapter 28 pandas Exercise Solutions

these are from interviewquery

28.0.0.1 Question

import pandas as pd

name_list = ["Tim Voss", "Nicole Johnson", "Elsa Williams", "John James", "Catherine Jones"]
age_list = [19, 20, 21, 20, 23]
color_list = ["red", "yellow", "green", "blue", "green"]
grades = [91, 95, 82, 75, 93]


students = {"name" : name_list,
            "age" : age_list,
            "favorite_color" : color_list,
            "grade" : grades}

students_df = pd.DataFrame(students)

students_df
##               name  age favorite_color  grade
## 0         Tim Voss   19            red     91
## 1   Nicole Johnson   20         yellow     95
## 2    Elsa Williams   21          green     82
## 3       John James   20           blue     75
## 4  Catherine Jones   23          green     93

Write a function named grades_colors to select only the rows where the student’s favorite color is green or red and their grade is above 90.

def grades_colors(df):
  
    df = df[(df['favorite_color'].isin(['green', 'red'])) & (df['grade'] > 90)]
  
    return df

grades_colors(students_df)
##               name  age favorite_color  grade
## 0         Tim Voss   19            red     91
## 4  Catherine Jones   23          green     93

Alternative

students_df.query("favorite_color.isin(('green', 'red')) and grade > 90")
##               name  age favorite_color  grade
## 0         Tim Voss   19            red     91
## 4  Catherine Jones   23          green     93

Using query method

colors = ["green", "red"]

students_df.query("favorite_color in @colors").query("grade > 90")
##               name  age favorite_color  grade
## 0         Tim Voss   19            red     91
## 4  Catherine Jones   23          green     93

Using loc

students_df.loc[(students_df['favorite_color'].isin(['green', 'red'])) &      
                (students_df['grade'] > 90)
                ]
##               name  age favorite_color  grade
## 0         Tim Voss   19            red     91
## 4  Catherine Jones   23          green     93

Alternative

color = students_df['favorite_color'].isin(['green', 'red'])
grade = students_df['grade'] > 90

students_df.loc[(grade) & (color)]

28.0.0.2 Question

You are given a dataframe with a single column, ‘var’.

Calculated the t-value for the mean of ‘var’ against a null hypothesis that \(\mu = \mu_0\)

Note: You do not have to calculate the p-value of the test or run the test.

var_data = [2,3,4,5,6,7,8,8,10]

df = pd.DataFrame({"var": var_data})

mu_0 = 5

def t_score(mu_0, df):
  
    n = df['var'].count()
    
    sample_mean = df['var'].mean()
    
    sample_std = df['var'].std()
    
    t = (sample_mean - mu_0) / (sample_std / pow(n, 1/2))
    
    return t

t_score(mu_0, df)
## 1.018055620761245

28.0.1 Question

Given a dataframe with three columns: client_id, ranking, value

Write a function to fill the NaN values in the value column with the previous non-NaN value from the same client_id ranked in ascending order.

If there doesn’t exist a previous client_id then return the previous value.

client_id = [1001, 1001, 1001, 1002, 1002, 1002, 1003, 1003]
ranking = [1, 2, 3, 1, 2, 3, 1, 2]
value = [1000, pd.NA, 1200, 1500, 1250, pd.NA, 1100, pd.NA]

clients_df = pd.DataFrame({
                  'client_id': client_id,
                  'ranking': ranking,
                  'value': value
})


def previous_nan_values(clients_df):
  
  clients_df = clients_df.sort_values(by=['client_id', 'ranking'])
  
  clients_df['value2'] = clients_df.groupby('client_id')['value'].ffill()
  
  return clients_df
  
previous_nan_values(clients_df)
##    client_id  ranking value value2
## 0       1001        1  1000   1000
## 1       1001        2  <NA>   1000
## 2       1001        3  1200   1200
## 3       1002        1  1500   1500
## 4       1002        2  1250   1250
## 5       1002        3  <NA>   1250
## 6       1003        1  1100   1100
## 7       1003        2  <NA>   1100