diff --git a/Calculate Special Bonus b/Calculate Special Bonus new file mode 100644 index 0000000..d83cfd3 --- /dev/null +++ b/Calculate Special Bonus @@ -0,0 +1,18 @@ +import pandas as pd + +def calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame: + # result =[] + # for i in range (len(employees)): + # e_id = employees['employee_id'][i] + # name = employees['name'][i] + # if (e_id%2 !=0) and (name[0] != 'M'): + # result.append([e_id, employees['salary'][i]]) + # else: + # result.append([e_id,0]) + # return pd.DataFrame(result,columns = ['employee_id', 'bonus']).sort_values(by=['employee_id']) + #df =employees['name'] + +#df[''] ==df[''] is an iterable object -->Series +#from df extract a list + employees['bonus'] =employees.apply(lambda x: x['salary'] if x ['employee_id'] % 2 and not x['name'].startswith('M') else 0, axis =1) + return employees[['employee_id','bonus']].sort_values(by ='employee_id') diff --git a/Fix Names in a Table b/Fix Names in a Table new file mode 100644 index 0000000..17940a4 --- /dev/null +++ b/Fix Names in a Table @@ -0,0 +1,9 @@ +import pandas as pd + +def fix_names(users: pd.DataFrame) -> pd.DataFrame: + # users['name'] = users['name'].str[0].str.upper() + users['name'].str[1:].str.lower() + # return users.sort_values(by=['user_id']) + + #easy way - use capitalize method. + users['name'] = users['name'].str.capitalize() + return users.sort_values(by=['user_id']) diff --git a/Patients With a Condition b/Patients With a Condition new file mode 100644 index 0000000..482b59e --- /dev/null +++ b/Patients With a Condition @@ -0,0 +1,28 @@ +import pandas as pd + +def find_patients(patients: pd.DataFrame) -> pd.DataFrame: + #condition - diab1 is there for diabetes + #follwoing is a long way of solving the query + #1 + + # result =[] + # for i in range(len(patients)): + # p_id = patients['patient_id'][i] + # p_name=patients['patient_name'][i] + # conditions =patients['conditions'][i] + # for condition in conditions.split(): + # if conditions.startswith('DIAB1'): + # result.append([p_id,p_name,condition]) + # break + # return pd.DataFrame(result,columns =['patient_id', 'patient_name','conditions']) + + #a little shorter approach + + #2 + df = patients[(patients['conditions'].str.startswith('DIAB1')) | (patients['conditions'].str.contains('DIAB1'))] + return df + + #the most simplest way - + #3 + #return patients[patients['conditions'].str.contains(r"(^|\s)DIAB1", regex=True)] +