diff --git a/CalculateSpecialBonus.py b/CalculateSpecialBonus.py new file mode 100644 index 0000000..b8b3463 --- /dev/null +++ b/CalculateSpecialBonus.py @@ -0,0 +1,26 @@ +#Method 1: +import pandas as pd + +def calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame: + + result = [] + for i in range(len(employees)): + eid = employees['employee_id'][i] + ename = employees['name'][i] + esal = employees['salary'][i] + + if eid % 2 == 1 and ename[0] != 'M': + bonus = esal + else: + bonus = 0 + result.append([eid,bonus]) + return pd.DataFrame(result, columns = ['employee_id','bonus']).sort_values(['employee_id']) + +#Method 2: + +import pandas as pd + +def calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame: + condition = (employees['employee_id'] % 2 == 1) & (~employees['name'].str.startswith('M')) + employees['bonus'] = employees['salary'].where(condition,0) + return employees[['employee_id','bonus']].sort_values(by = ['employee_id']) \ No newline at end of file diff --git a/FixNamesInATable.py b/FixNamesInATable.py new file mode 100644 index 0000000..8cf34b1 --- /dev/null +++ b/FixNamesInATable.py @@ -0,0 +1,29 @@ +# Method 1 +import pandas as pd + +def fix_names(users: pd.DataFrame) -> pd.DataFrame: + result = [] + for i in range(len(users)): + name = users['name'][i] + uid = users['user_id'][i] + + new_name = name[0].upper() + name[1:].lower() + + result.append([uid,new_name]) + return pd.DataFrame(result, columns = ['user_id','name']).sort_values(['user_id']) + +# # Method 2 + +import pandas as pd + +def fix_names(users: pd.DataFrame) -> pd.DataFrame: + result = [] + for i in range(len(users)): + name = users['name'][i] + uid = users['user_id'][i] + + new_name = name.capitalize() + # new_name is taking one string at a time. If we had taken the whole column users['name'] then we would write it as name.str.capitalize() + + result.append([uid,new_name]) + return pd.DataFrame(result, columns = ['user_id','name']).sort_values(['user_id']) diff --git a/PatientsWithACondition.py b/PatientsWithACondition.py new file mode 100644 index 0000000..dd04f0c --- /dev/null +++ b/PatientsWithACondition.py @@ -0,0 +1,5 @@ +import pandas as pd + +def find_patients(patients: pd.DataFrame) -> pd.DataFrame: + cond = (patients['conditions'].str.startswith('DIAB1'))|(patients['conditions'].str.contains(' DIAB1')) + return patients[cond] \ No newline at end of file