From ead4ade090dc423b38b2f9f23211642827a788ee Mon Sep 17 00:00:00 2001 From: NehaDhaliwalNehaDhaliwal Date: Fri, 7 Mar 2025 15:41:39 -0600 Subject: [PATCH] Done Pandas3 --- Problem 1.py | 16 ++++++++++++++++ Problem 2.py | 12 ++++++++++++ Problem 3.py | 12 ++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 Problem 1.py create mode 100644 Problem 2.py create mode 100644 Problem 3.py diff --git a/Problem 1.py b/Problem 1.py new file mode 100644 index 0000000..76882c1 --- /dev/null +++ b/Problem 1.py @@ -0,0 +1,16 @@ +# Calculate Special Bonus + +import pandas as pd + +def calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame: + employees['bonus'] = employees.apply(lambda x : x['salary'] + if x['employee_id'] % 2 and + not x['name'].startswith('M') + else 0, axis = 1) + df = employees[['employee_id', 'bonus']].sort_values('employee_id') + print(df) + return df + +data = [[2, 'Meir', 3000], [3, 'Michael', 3800], [7, 'Addilyn', 7400], [8, 'Juan', 6100], [9, 'Kannon', 7700]] +employees = pd.DataFrame(data, columns=['employee_id', 'name', 'salary']).astype({'employee_id':'int64', 'name':'object', 'salary':'int64'}) +calculate_special_bonus(employees) \ No newline at end of file diff --git a/Problem 2.py b/Problem 2.py new file mode 100644 index 0000000..7f72d8e --- /dev/null +++ b/Problem 2.py @@ -0,0 +1,12 @@ +# Fix Names in a Table + +import pandas as pd + +def fix_names(users: pd.DataFrame) -> pd.DataFrame: + users['name'] = users['name'].str.capitalize() + print(users) + return users.sort_values(by = 'user_id') + +data = [[1, 'aLice'], [2, 'bOB']] +users = pd.DataFrame(data, columns=['user_id', 'name']).astype({'user_id':'Int64', 'name':'object'}) +fix_names(users) \ No newline at end of file diff --git a/Problem 3.py b/Problem 3.py new file mode 100644 index 0000000..ddff4bf --- /dev/null +++ b/Problem 3.py @@ -0,0 +1,12 @@ +# Patients with a Condition + +import pandas as pd + +def find_patients(patients: pd.DataFrame) -> pd.DataFrame: + patients = patients[patients['conditions'].str.startswith('DIAB1') | patients['conditions'].str.contains(' DIAB1')] + print(patients) + return patients + +data = [[1, 'Daniel', 'YFEV COUGH'], [2, 'Alice', ''], [3, 'Bob', 'DIAB100 MYOP'], [4, 'George', 'ACNE DIAB100'], [5, 'Alain', 'DIAB201']] +patients = pd.DataFrame(data, columns=['patient_id', 'patient_name', 'conditions']).astype({'patient_id':'int64', 'patient_name':'object', 'conditions':'object'}) +find_patients(patients) \ No newline at end of file