Advanced-Custom-Lambda
Sat 17 May 2025
title: "Advanced Custom Lambda" author: "Rj" date: 2019-04-22 description: "-" type: technical_note draft: false
import numpy as np
import pandas as pd
# Check the student passed either math or passed at least in 2 subjects. If no condidition matched, consider them as failed
def pass_math_or_two_subjects(row):
if(row.maths > 34):
return 'Pass'
if(row.language > 34 and row.science > 34):
return 'Pass'
return 'Fail'
df = pd.read_csv('abc.csv')
df
| student | language | science | maths | history | |
|---|---|---|---|---|---|
| 0 | kumar | 90 | 56 | 34 | 34 |
| 1 | kevin | 10 | 34 | 32 | 67 |
| 2 | sammy | 90 | 23 | 12 | 32 |
| 3 | janice | 20 | 67 | 90 | 45 |
| 4 | peter | 30 | 56 | 45 | 65 |
| 5 | prem | 90 | 45 | 45 | 34 |
| 6 | carrol | 50 | 90 | 45 | 23 |
df['special_result'] = df.apply(pass_math_or_two_subjects, axis=1)
df
| student | language | science | maths | history | special_result | |
|---|---|---|---|---|---|---|
| 0 | kumar | 90 | 56 | 34 | 34 | Pass |
| 1 | kevin | 10 | 34 | 32 | 67 | Fail |
| 2 | sammy | 90 | 23 | 12 | 32 | Fail |
| 3 | janice | 20 | 67 | 90 | 45 | Pass |
| 4 | peter | 30 | 56 | 45 | 65 | Pass |
| 5 | prem | 90 | 45 | 45 | 34 | Pass |
| 6 | carrol | 50 | 90 | 45 | 23 | Pass |
# Filter those candidates whoever passed the given criteria
df_passed = df[df.apply(pass_math_or_two_subjects, axis=1) == 'Pass']
df_passed
| student | language | science | maths | history | special_result | |
|---|---|---|---|---|---|---|
| 0 | kumar | 90 | 56 | 34 | 34 | Pass |
| 3 | janice | 20 | 67 | 90 | 45 | Pass |
| 4 | peter | 30 | 56 | 45 | 65 | Pass |
| 5 | prem | 90 | 45 | 45 | 34 | Pass |
| 6 | carrol | 50 | 90 | 45 | 23 | Pass |
Score: 5
Category: data-wrangling