Skip to content

Intermediate Python

Run the hidden code cell below to import the data used in this course.


1 hidden cell
import pandas as pd

Add your notes here

# Add your code snippets here
# importing libraries
import warnings
warnings.filterwarnings('ignore')

import pandas as pd
import numpy as np

# Importing data
excel_file_path = 'C:\\Users\\francis.odior\\OneDrive - Palladium International, LLC\\Desktop\\Data gap Analysis\\data.xlsx'

Outpatient and General Attendance
att = excel_file_path[['LGA', 'Ward', 'Facility Name', 'periodname','General Attendance','Out-patient Attendance',
                   'Diarrhoea new cases < 5 years',
                   'Diarrhoea new cases <5 years - given ORS and zinc Female',
                   'Diarrhoea new cases <5 years - given ORS and zinc Male',
                   'Pneumonia new cases < 5 years',
                   'Pneumonia new cases < 5 years - given amoxyl DT Female',
                   'Pneumonia new cases < 5 years - given amoxyl DT Male']]
dtreatment = ['Diarrhoea new cases <5 years - given ORS and zinc Female',
              'Diarrhoea new cases <5 years - given ORS and zinc Male']

Explore Datasets

Use the DataFrames imported in the first cell to explore the data and practice your skills!

  • Create a loop that iterates through the brics DataFrame and prints "The population of {country} is {population} million!".
  • Create a histogram of the life expectancies for countries in Africa in the gapminder DataFrame. Make sure your plot has a title, axis labels, and has an appropriate number of bins.
  • Simulate 10 rolls of two six-sided dice. If the two dice add up to 7 or 11, print "A win!". If the two dice add up to 2, 3, or 12, print "A loss!". If the two dice add up to any other number, print "Roll again!".
importing libraries
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import numpy as np
Importing data
excel_file_path = 'C:\\Users\\francis.odior\\OneDrive - Palladium International, LLC\\Desktop\\Data gap Analysis\\data.xlsx'
Outpatient and General Attendance
att = excel_file_path[['LGA', 'Ward', 'Facility Name', 'periodname','General Attendance','Out-patient Attendance',
                   'Diarrhoea new cases < 5 years',
                   'Diarrhoea new cases <5 years - given ORS and zinc Female',
                   'Diarrhoea new cases <5 years - given ORS and zinc Male',
                   'Pneumonia new cases < 5 years',
                   'Pneumonia new cases < 5 years - given amoxyl DT Female',
                   'Pneumonia new cases < 5 years - given amoxyl DT Male']]
dtreatment = ['Diarrhoea new cases <5 years - given ORS and zinc Female',
              'Diarrhoea new cases <5 years - given ORS and zinc Male']

att['dtreatment'] = data[dtreatment].sum(axis=1)
ptreatment = ['Pneumonia new cases < 5 years - given amoxyl DT Female',
              'Pneumonia new cases < 5 years - given amoxyl DT Male']

att['ptreatment'] = data[ptreatment].sum(axis=1)
Validation Rules
conditions = [att['Out-patient Attendance'] > att['General Attendance']]  
choices = ['Outpatient attendance is more than General attendance']  
att['att_Remarks-1'] = np.select(conditions,choices, default='')
conditions = [att['Out-patient Attendance'] == att['General Attendance']]  
choices = ['Outpatient attendance is equal to General attendance']  
att['att_Remarks-2'] = np.select(conditions,choices, default='')
conditions = [(att['Out-patient Attendance'] == 0) & (att['General Attendance']==0)]  
choices = ['Outpatient & General attendance was not reported']  
att['att_Remarks-3'] = np.select(conditions,choices, default='')
conditions = [att['dtreatment'] > att['Diarrhoea new cases < 5 years']]  
choices = ['diarrhoea(treatment) is more than diarrhoea(cases)']  
att['diarrhoea_Remarks-1'] = np.select(conditions,choices, default='')
conditions = [att['ptreatment'] > att['Pneumonia new cases < 5 years']]  
choices = ['Pneuemonia(treatment) is more than Pneumonia (cases)']  
att['pneumonia_Remarks-2'] = np.select(conditions,choices, default='')
att = att.drop(att[(att['att_Remarks-1'] == '') & 
                   (att['att_Remarks-2'] == '') & 
                   (att['att_Remarks-3'] == '') & 
                   (att['diarrhoea_Remarks-1'] == '') &
                   (att['pneumonia_Remarks-2'] == '')].index)
Remarks1 = att[['LGA', 'Ward', 'Facility Name', 'periodname','att_Remarks-1', 'att_Remarks-2', 'att_Remarks-3', 
                'diarrhoea_Remarks-1','pneumonia_Remarks-2']]
melt = Remarks1.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/att_imci_remarks_all.xls', index = False)
Antenetal Care
data = data[['LGA','Ward','Facility Name','periodname',
              'ANC 1st Visit  GA < 20wks', 'ANC 1st Visit  GA ≥ 20wks',
               'ANC 4th visit', 'ANC Attendance  15 - 19yrs',
               'ANC Attendance 10 - 14yrs', 'ANC Attendance',
               'ANC Syphilis case treated', 'ANC Syphilis test done ',
               'ANC Syphilis test positive ', 'ANC Hep. C test done',
               'ANC Hep. B test done', 'PW Counselled on FP',
               'PW Counselled on Maternal Nutrition', 'PW counselled on FGM', 'IPT1p',
               'IPT2p', 'IPT3p', 'PW who received Haematinics', 'PW who received LLIN',
             'Pregnant women given TD vaccine TD1',
             'Live Births Female, <2.5kg', 'Live Births Female, >2.5kg',
             'Live Births Male, <2.5kg', 'Live Births Male, >2.5kg',
             'Postnatal Clinic Visits Newborns, 1d',
             'Postnatal Clinic Visits Newborns, 2 - 3d',
             'Postnatal Clinic Visits Newborns, 4 - 7d',
             'Postnatal Clinic Visits Newborns, >7d', 'Deaths Maternal',
             'Postnatal Clinic Visits Mothers, 1d',
             'Postnatal Clinic Visits Mothers, 2 - 3d',
             'Postnatal Clinic Visits Mothers, 4 - 7d',
             'Postnatal Clinic Visits Mothers, >7d', 
             'Newborns with danger signs Female', 'Newborns with danger signs Male',
             'Newborns with danger signs given 1st dose of antibiotics and referred Female',
             'Newborns with danger signs given 1st dose of antibiotics and referred Male']]
Newborn_with_danger_signs = ['Newborns with danger signs Female', 'Newborns with danger signs Male']

data['Newborn_with_danger_signs'] = data[Newborn_with_danger_signs].sum(axis=1)
Newborn_with_danger_signs_given_antibiotics = ['Newborns with danger signs given 1st dose of antibiotics and referred Female',
                                              'Newborns with danger signs given 1st dose of antibiotics and referred Male']

data['Newborn_with_danger_signs_given_antibiotics'] = data[Newborn_with_danger_signs_given_antibiotics].sum(axis=1)
pnc_newborntotal = ['Postnatal Clinic Visits Newborns, 1d',
                    'Postnatal Clinic Visits Newborns, 2 - 3d',
                    'Postnatal Clinic Visits Newborns, 4 - 7d',
                    'Postnatal Clinic Visits Newborns, >7d']

data['pnc_newborntotal'] = data[pnc_newborntotal].sum(axis=1)
pnc_mothertotal = ['Postnatal Clinic Visits Mothers, 1d',
                   'Postnatal Clinic Visits Mothers, 2 - 3d',
                   'Postnatal Clinic Visits Mothers, 4 - 7d',
                   'Postnatal Clinic Visits Mothers, >7d']

data['pnc_mothertotal'] = data[pnc_mothertotal].sum(axis=1)            
anc_1st_visit = ['ANC 1st Visit  GA < 20wks','ANC 1st Visit  GA ≥ 20wks']

data['anc_1st_visit'] = data[anc_1st_visit].sum(axis=1)
livebirth = ['Live Births Female, <2.5kg','Live Births Female, >2.5kg', 
                   'Live Births Male, <2.5kg','Live Births Male, >2.5kg']

data['livebirth'] = data[livebirth].sum(axis=1)
anc= data[['LGA', 'Ward', 'Facility Name',
            'periodname', 'ANC 1st Visit  GA < 20wks',
            'ANC 1st Visit  GA ≥ 20wks', 'anc_1st_visit','ANC 4th visit','ANC Hep. C test done',
            'ANC Attendance', 'PW who received Haematinics','PW Counselled on Maternal Nutrition',
            'ANC Syphilis case treated','ANC Syphilis test done ','ANC Syphilis test positive ','ANC Hep. B test done', 
            'IPT1p','IPT2p', 'IPT3p','Pregnant women given TD vaccine TD1','PW Counselled on FP',
            'Newborn_with_danger_signs','Newborn_with_danger_signs_given_antibiotics','PW counselled on FGM',
            'pnc_mothertotal','pnc_newborntotal','livebirth','Postnatal Clinic Visits Newborns, 1d']]
validation rules ----antenatal care
conditions = [anc['IPT1p'] > anc['anc_1st_visit']]  
choices = ['IPT-1 given to PW is more than ANC 1st visit']  
anc['anc_Remarks-1(melworkflow)'] = np.select(conditions,choices, default='')
conditions = [anc['IPT1p'] < anc['anc_1st_visit']]   
choices = ['IPT-1 given to PW is less than ANC 1st visit']  
anc['anc_Remarks-2(melworkflow)'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Attendance'] > anc['PW who received Haematinics']]   
choices = ['Haematinics given to PW is less than reported total ANC']  
anc['anc_Remarks-3'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Attendance'] < anc['PW who received Haematinics']]   
choices = ['Haematinics given to PW is more than reported total ANC']  
anc['anc_Remarks-4'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Syphilis test done '] > anc['ANC Syphilis test positive ']]   
choices = ['Syphilis (Positive) is more than Syphilis(Test)']  
anc['anc_Remarks-5(melworkflow)'] = np.select(conditions,choices, default='')
conditions = [anc['ANC 4th visit'] > anc['anc_1st_visit']]   
choices = ['ANC 4th visit is more than total ANC-1']  
anc['anc_Remarks-6(melworkflow)'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Attendance']== 0]  
choices = ['anc services was not reported']  
anc['anc_Remarks-7'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Syphilis case treated'] > anc['ANC Syphilis test positive ']]   
choices = ['Syphilis(Treatment) is more than Syphilis(Positive)']  
anc['anc_Remarks-8'] = np.select(conditions,choices, default='')
conditions = [anc['PW Counselled on FP'] > anc['ANC Attendance']]   
choices = ['PW counselled on FP is more than ANC Attendance']  
anc['anc_Remarks-9'] = np.select(conditions,choices, default='')
conditions = [anc['PW Counselled on Maternal Nutrition'] > anc['ANC Attendance']]   
choices = ['PW counselled on Maternal Nutrition is more than ANC Attendance']  
anc['anc_Remarks-10'] = np.select(conditions,choices, default='')
conditions = [anc['PW counselled on FGM'] > anc['ANC Attendance']]   
choices = ['PW counselled on FGM is more than ANC Attendance']  
anc['anc_Remarks-11'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Hep. C test done'] > anc['anc_1st_visit']]   
choices = ['ANC Hep C test is more than ANC 1st visit']  
anc['anc_Remarks-12'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Hep. B test done'] > anc['anc_1st_visit']]   
choices = ['ANC Hep C test is more than ANC 1st visit']  
anc['anc_Remarks-13'] = np.select(conditions,choices, default='')
conditions = [anc['ANC Syphilis test done '] > anc['anc_1st_visit']]   
choices = ['ANC Syphilis test is more than ANC 1st visit']  
anc['anc_Remarks-14'] = np.select(conditions,choices, default='')
validation rules ----postnatal care
conditions = [(anc['pnc_mothertotal'] > anc['pnc_newborntotal'])]   
choices = ['mothers that came for pnc are reported to be more than newborns']  
anc['pnc_Remarks-1'] = np.select(conditions,choices, default='')
conditions = [(anc['Newborn_with_danger_signs'] < anc['Newborn_with_danger_signs_given_antibiotics'])]   
choices = ['Antibiotics given is more than newborn danger signs']  
anc['pnc_Remarks-2(melworkflow)'] = np.select(conditions,choices, default='')
conditions = [(anc['Postnatal Clinic Visits Newborns, 1d'] < anc['livebirth'])]  
choices = ['PNC Day 1(Newborn) reported is less than Livebirths)']  
anc['pnc_Remarks-3'] = np.select(conditions,choices, default='')
anc = anc.drop(anc[(anc['anc_Remarks-1(melworkflow)'] == '') & (anc['anc_Remarks-2(melworkflow)'] == '') & 
                   (anc['anc_Remarks-3'] == '') & (anc['anc_Remarks-4'] == '') &
                   (anc['anc_Remarks-5(melworkflow)'] == '') & (anc['anc_Remarks-6(melworkflow)'] == '') &
                   (anc['anc_Remarks-7'] == '') & (anc['anc_Remarks-8'] == '') &
                   (anc['anc_Remarks-9'] == '') & (anc['anc_Remarks-10'] == '')&
                   (anc['anc_Remarks-11'] == '')& (anc['anc_Remarks-12'] == '')&
                   (anc['anc_Remarks-13'] == '')& (anc['anc_Remarks-14'] == '')&
                   (anc['pnc_Remarks-1'] == '') & (anc['pnc_Remarks-2(melworkflow)'] == '') & 
                   (anc['pnc_Remarks-3'] == '')].index)
Remarks- anc and pnc
Remarks1 = anc[['LGA', 'Ward', 'Facility Name', 'periodname',
                'anc_Remarks-1(melworkflow)', 'anc_Remarks-2(melworkflow)', 'anc_Remarks-3', 'anc_Remarks-4', 'anc_Remarks-5(melworkflow)',
                'anc_Remarks-6(melworkflow)', 'anc_Remarks-7', 'anc_Remarks-8',
                'anc_Remarks-9', 'anc_Remarks-10', 'anc_Remarks-11',
                'anc_Remarks-12', 'anc_Remarks-13', 'anc_Remarks-14',
                'pnc_Remarks-1','pnc_Remarks-2(melworkflow)','pnc_Remarks-3']]
melt = Remarks1.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/anc_pnc_remarks_all.xls', index = False)
 
Delivery Outcomes
data=pd.read_excel(r'C:\Users\Umar Muhammad\Dropbox\T03 Bauchi\FY22\Q3\february_march\cleaning_dhis2\data.xlsx')
mnh = data[['Facility Name','LGA', 'Ward', 'periodname',
               'Deliveries Assisted', 'Deliveries Caeserean Section',
               'Deliveries Spontaneous Vaginal Delivery (SVD)',
               'Women counselled on Postpartum FP',
               'Deliveries by Skilled Birth Attendants (SBA)',
               'Deliveries monitored using a partograph',
               'Women given Uterotonics in the 3rd stage of labour Misoprostol',
               'Women given Uterotonics in the 3rd stage of labour Oxytocin',
               'Live Births Female, <2.5kg', 'Live Births Female, >2.5kg',
               'Live Births Male, <2.5kg', 'Live Births Male, >2.5kg',
               'Still birth  Fresh Still Births (FSB)', 'Still birth  Macerated (MSB)']]
#total deliveries

total_deliveries= ['Deliveries Assisted',
                    'Deliveries Caeserean Section',
                    'Deliveries Spontaneous Vaginal Delivery (SVD)']

mnh['total_deliveries']=mnh[total_deliveries].sum(axis=1)
#livebirth
total_livebirths = ['Live Births Female, <2.5kg','Live Births Female, >2.5kg','Live Births Male, <2.5kg',
                    'Live Births Male, >2.5kg']
mnh['total_livebirths'] = mnh[total_livebirths].sum(axis=1)
#still birth
total_stillbirths = ['Still birth  Fresh Still Births (FSB)','Still birth  Macerated (MSB)']                 
mnh['total_stillbirths'] = mnh[total_stillbirths].sum(axis=1)
#total outcome
total_outcomes = mnh['total_outcomes']=mnh[['total_livebirths','total_stillbirths']].sum(axis=1)
Remarks
#Creating a remarks columns 

conditions = [mnh['Deliveries Assisted'] > 0]
choices=['assisted delivery reported, (secondary facility; its fine)']
mnh['L&D_Remarks-1'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries Assisted'] > mnh['Deliveries Spontaneous Vaginal Delivery (SVD)']]
choices=['assisted delivery is more than Spontaneous vaginal delivery']
mnh['L&D_Remarks-2(melworkflow)'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries Caeserean Section'] > mnh['Deliveries Spontaneous Vaginal Delivery (SVD)']]
choices=['Caeserean Section is more than Spontaneous vaginal delivery']
mnh['L&D_Remarks-3(melworkflow)'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['total_deliveries'] == 0]
choices=['no delivery reported']
mnh['L&D_Remarks-4'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['total_livebirths'] == 0]
choices=['Livebirths was not reported']
mnh['L&D_Remarks-5'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['total_deliveries'] > mnh['total_outcomes']]
choices=['deliveries are more than reported outcomes(livebirth & stillbirth)']
mnh['L&D_Remarks-6(melworkflow)'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries by Skilled Birth Attendants (SBA)'] < mnh['total_deliveries']]
choices=['SBA reported is less than total deliveries']
mnh['L&D_Remarks-7'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries by Skilled Birth Attendants (SBA)'] > mnh['total_deliveries']]
choices=['SBA reported is more than total deliveries']
mnh['L&D_Remarks-8'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries by Skilled Birth Attendants (SBA)'] == 0 ]
choices=['SBA reported was not reported']
mnh['L&D_Remarks-9'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries monitored using a partograph'] == 0 ]
choices=['use of partograph not reported']
mnh['L&D_Remarks-10'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries monitored using a partograph'] > mnh['total_deliveries']]
choices=['use of partograph reported is more than reported delilveries']
mnh['L&D_Remarks-11(melworkflow)'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Deliveries monitored using a partograph'] < mnh['total_deliveries']]
choices=['use of partograph reported is less than reported delilveries']
mnh['L&D_Remarks-12'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Women counselled on Postpartum FP'] == 0 ]
choices=['Postpatum FP counselling was not reported']
mnh['L&D_Remarks-13'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Women counselled on Postpartum FP'] > mnh['total_deliveries']]
choices=['Women Couselled on PPFP is more than total deliveries']
mnh['L&D_Remarks-14(melworkflow)'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [mnh['Women counselled on Postpartum FP'] < mnh['total_deliveries']]
choices=['Women Counselled on PPFP is less than total deliveries']
mnh['L&D_Remarks-15(melworkflow)'] = np.select(conditions,choices, default='')
mnh = mnh.drop(mnh[(mnh['L&D_Remarks-1'] == '') & 
                   (mnh['L&D_Remarks-2(melworkflow)'] == '') & 
                   (mnh['L&D_Remarks-3(melworkflow)'] == '') &
                   (mnh['L&D_Remarks-4'] == '') &
                   (mnh['L&D_Remarks-5'] == '') &
                   (mnh['L&D_Remarks-6(melworkflow)'] == '') &
                   (mnh['L&D_Remarks-7'] == '') &
                   (mnh['L&D_Remarks-8'] == '') &
                   (mnh['L&D_Remarks-9'] == '') &
                   (mnh['L&D_Remarks-10'] == '') &
                   (mnh['L&D_Remarks-11(melworkflow)'] == '') &
                   (mnh['L&D_Remarks-12'] == '') &
                   (mnh['L&D_Remarks-13'] == '') &
                   (mnh['L&D_Remarks-14(melworkflow)'] == '') &
                   (mnh['L&D_Remarks-15(melworkflow)'] == '')].index)
Remarks = mnh[['Facility Name','LGA', 'Ward', 'periodname',
                'L&D_Remarks-1', 'L&D_Remarks-2(melworkflow)', 'L&D_Remarks-3(melworkflow)','L&D_Remarks-4', 
                'L&D_Remarks-5', 'L&D_Remarks-6(melworkflow)', 'L&D_Remarks-7', 'L&D_Remarks-8',
                'L&D_Remarks-9', 'L&D_Remarks-10', 'L&D_Remarks-11(melworkflow)', 'L&D_Remarks-12',
                'L&D_Remarks-13','L&D_Remarks-14(melworkflow)','L&D_Remarks-15(melworkflow)']]
melt = Remarks.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/delivery_outcomes_remarks_all.xls', index = False)
 
Maternal and Neonatal Deaths_seek Clarification from Samson to finalize
death = data[['Facility Name','LGA', 'Ward', 'periodname','Deaths Maternal',
                       'Deaths Neonatal', 'Maternal Deaths ', 'Deaths Maternal Abortion',
                       'Deaths Maternal Anaemia', 'Deaths Maternal HIV',
                       'Deaths Maternal Malaria', 'Deaths Maternal Obstructed Labour',
                       'Deaths Maternal Others', 'Deaths Maternal PPH',
                       'Deaths Maternal Sepsis', 'Deaths Neonatal Congenital Malformation',
                       'Deaths Neonatal Others - Neonate', 'Deaths Neonatal Prematurity',
                       'Deaths Neonatal Tetanus', 'Deaths < 5 Malaria - U5',
                       'Deaths < 5 Malnutrition', 'Deaths < 5 Others - U5']]
#maternal death types

types_maternaldeaths = death['types_maternaldeaths']= death[['Deaths Maternal Abortion','Deaths Maternal Anaemia',
                                                            'Deaths Maternal HIV','Deaths Maternal Malaria',
                                                            'Deaths Maternal Obstructed Labour','Deaths Maternal Others',
                                                            'Deaths Maternal PPH','Deaths Maternal Sepsis']].sum(axis=1)
#neonatal death types

types_neonataldeaths = death['types_neonataldeaths']= death[['Deaths Neonatal Congenital Malformation',
                                                             'Deaths Neonatal Others - Neonate', 'Deaths Neonatal Prematurity',
                                                             'Deaths Neonatal Tetanus']].sum(axis=1)
Validation Rules
#Creating a remarks columns 

conditions = [death['Deaths Maternal'] > 0 ]
choices=['maternal death reported, validate']
death['md_Remarks-1'] = np.select(conditions,choices, default='')
#Creating a remarks columns 

conditions = [death['Deaths Neonatal'] > 0 ]
choices=['neonatal death report, validate']
death['nd_Remarks-2'] = np.select(conditions,choices, default='')
death = death.drop(death[(death['md_Remarks-1'] == '') &  (death['nd_Remarks-2'] == '')].index)
Remarks = death[['Facility Name','LGA', 'Ward', 'periodname','md_Remarks-1', 'nd_Remarks-2']]
melt = Remarks.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/death_remarks_all.xls', index = False)
Family Planning
fp=data[['Facility Name', 'LGA', 'Ward',  
         'periodname','Females using modern contraception',
         'New FP Acceptors Female', 'New FP Acceptors Male',
         'Females using modern contraception 10 - 14yrs',
         'Females using modern contraception 15-19yrs',
         'Females using modern contraception 20 - 24yrs',
         'Females using modern contraception 25 - 49 yrs',
         'Females using modern contraception ≥ 50yrs',
         'Women counselled on Postpartum FP', 'FP clients counselled Female',
         'FP clients counselled Male', 'Condoms distributed- Female',
         'Condoms distributed- Male', 'Clients given oral pills',
         'Oral pills cycle (sachets) dispensed',
         'Emergency contraceptive pills dispensed',
         'Injectables given Noristerat', 'Injectables given DMPA-IM',
         'Injectables given Provider administer  DMPA-SC',
         'Injectables given Women self inject DMPA-SC',
         'IUD inserted  10yrs CuT 380A (Copper)',
         'IUD inserted  5yrs LNG IUS (Hormonal)',
         'Implants inserted Implanon NXT', 'Implants inserted Jadelle',
         'IUD inserted ', 'Post-partum IUD inserted',
         'Post-partum Implanon NXT inserted ', 'Post-partum Jadelle inserted',]]
total_clients_counselled = ['FP clients counselled Male','FP clients counselled Female']

fp['total_clients_counselled'] = fp[total_clients_counselled].sum(axis=1)
total_new_acceptors = ['New FP Acceptors Female','New FP Acceptors Male']

fp['total_new_acceptors'] = fp[total_new_acceptors].sum(axis=1)
total_female_using_mc = ['Females using modern contraception 10 - 14yrs',
                        'Females using modern contraception 15-19yrs',
                        'Females using modern contraception 20 - 24yrs',
                        'Females using modern contraception 25 - 49 yrs',
                        'Females using modern contraception ≥ 50yrs']

fp['total_female_using_mc'] = fp[total_female_using_mc].sum(axis=1)
mc_methods_utilized = ['Clients given oral pills','Condoms distributed- Female',
                       'Clients given oral pills','Implants inserted Implanon NXT', 'Implants inserted Jadelle',
                       'IUD inserted ','Injectables given Provider administer  DMPA-SC',
                       'Injectables given Noristerat','Injectables given DMPA-IM']

fp['mc_methods_utilized'] = fp[mc_methods_utilized].sum(axis=1)
total_implants = ['Implants inserted Implanon NXT', 'Implants inserted Jadelle']

fp['total_implants'] = fp[total_implants].sum(axis=1)
total_injectables = ['Injectables given Provider administer  DMPA-SC',
                     'Injectables given Noristerat', 
                     'Injectables given DMPA-IM']

fp['total_injectables'] = fp[total_injectables].sum(axis=1)
fp = fp[['Facility Name','LGA', 'Ward','periodname',
         'Condoms distributed- Male','Women counselled on Postpartum FP',
         'FP clients counselled Male','FP clients counselled Female',
          'total_clients_counselled','Clients given oral pills',
          'total_new_acceptors','total_injectables',
          'IUD inserted ','total_implants',
          'total_female_using_mc','mc_methods_utilized']]        
Validation Rules
conditions = [ fp['total_female_using_mc'] > fp['mc_methods_utilized']]
choices = ['fp methods utilized is less than females using mc methods']           
fp['fp_Remarks-1'] = np.select(conditions,choices, default='')
conditions = [ fp['total_female_using_mc'] < fp['mc_methods_utilized']]
choices = ['fp methods utilized is more than females using mc methods']            
fp['fp_Remarks-2'] = np.select(conditions,choices, default='')
conditions = [ fp['Women counselled on Postpartum FP'] > fp['FP clients counselled Female']]
choices = ['Counselled on PPFP is more than total females counselled on family planning']            
fp['fp_Remarks-3'] = np.select(conditions,choices, default='')
conditions = [ fp['Women counselled on Postpartum FP'] == fp['FP clients counselled Female']]
choices = ['Counselled on PPFP is the same as females counselled on family planning']            
fp['fp_Remarks-4'] = np.select(conditions,choices, default='')
fp = fp.drop(fp[(fp['fp_Remarks-1'] == '') & (fp['fp_Remarks-2'] == '') & (fp['fp_Remarks-3'] == '') & (fp['fp_Remarks-4'] == '')].index)
Remarks
Remarks = fp[['Facility Name','LGA', 'Ward', 'periodname','fp_Remarks-1','fp_Remarks-2','fp_Remarks-3']]
melt = Remarks.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/family_planning_remarks_all.xls', index = False)
 
Immunization
imm=data[['Facility Name', 'LGA', 'Ward','periodname','Penta. 1 given', 
          'Penta 3 given','Fully Immunized < 1 year','Measles 1 given' ]]
imm=imm.fillna(0)
penta_dropout = (imm["Penta. 1 given"] - imm["Penta 3 given"]).div(imm['Penta. 1 given'])

imm['penta_dropout']=penta_dropout
imm['dropout_rate']=pd.Series(["{0:.2f}%".format(val * 100) for val in imm['penta_dropout']], index = imm.index)
imm1=imm
Validations
conditions = [(imm['penta_dropout'] < 0)]
choices = ['negative dropout']
imm['Immunization_Remarks-1'] = np.select(conditions,choices, default='')
conditions = [(imm['Penta. 1 given'] == 0) & (imm['Penta 3 given'] == 0)]
choices = ['Penta 1 & 3 was not reports']
imm['Immunization_Remarks-2'] = np.select(conditions,choices, default='')
conditions = [(imm['penta_dropout'] >= 0.101 )]
choices = ['high dropout rate']
imm['Immunization_Remarks-3'] = np.select(conditions,choices, default='')
conditions = [(imm['Fully Immunized < 1 year']) > (imm['Measles 1 given'])]
choices = ['Fully Immunized is more than Measles 1']
imm['Immunization_Remarks-4'] = np.select(conditions,choices, default='')
Remarks= imm[['Facility Name', 'LGA', 'Ward', 'periodname','Immunization_Remarks-1',
              'Immunization_Remarks-2','Immunization_Remarks-3','Immunization_Remarks-4']]
melt = Remarks.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/Immunization_remarks_all.xls', index = False)
 
Immediate Newborn Care
#Sellecting Columns from a dataframe

new_born = data[['Facility Name','LGA','Ward','periodname',
                 'Live Births Female, <2.5kg', 'Live Births Female, >2.5kg',
                 'Live Births Male, <2.5kg', 'Live Births Male, >2.5kg',
                 'Still birth  Fresh Still Births (FSB)', 'Still birth  Macerated (MSB)',
                 '4% Chlorhexidine (CHX) gel is applied to cord at birth Female',
                 '4% Chlorhexidine (CHX) gel is applied to cord at birth Male',
                 'Babies not breathing/not crying at birth Female',
                 'Babies not breathing/not crying at birth Male',
                 'Babies not breathing/not crying at birth successfully resuscitated Female',
                 'Babies not breathing/not crying at birth successfully resuscitated Male',
                 'Babies put to breast within 1hr with skin-to-skin to keep warm Female',
                 'Babies put to breast within 1hr with skin-to-skin to keep warm Male']]
Importing data
total_CHx = ['4% Chlorhexidine (CHX) gel is applied to cord at birth Female',
                '4% Chlorhexidine (CHX) gel is applied to cord at birth Male']
new_born['total_CHx'] = new_born[total_CHx].sum(axis=1)
skin_skin_warm = ['Babies put to breast within 1hr with skin-to-skin to keep warm Female',
                     'Babies put to breast within 1hr with skin-to-skin to keep warm Male']
new_born['skin_skin_warm'] = new_born[skin_skin_warm].sum(axis=1)
nt_breathing = ['Babies not breathing/not crying at birth Female',
                 'Babies not breathing/not crying at birth Male',]
new_born['nt_breathing'] = new_born[nt_breathing].sum(axis=1)
successful_resus = ['Babies not breathing/not crying at birth successfully resuscitated Female',
                   'Babies not breathing/not crying at birth successfully resuscitated Male']
new_born['successful_resus'] = new_born[successful_resus].sum(axis=1)
livebirths = ['Live Births Female, <2.5kg','Live Births Female, >2.5kg','Live Births Male, <2.5kg','Live Births Male, >2.5kg']

new_born['livebirths'] = new_born[livebirths].sum(axis=1)
stillbirths = ['Still birth  Fresh Still Births (FSB)', 'Still birth  Macerated (MSB)']

new_born['stillbirths'] = new_born[stillbirths].sum(axis=1)
asphyxia = ['Babies not breathing/not crying at birth Female','Babies not breathing/not crying at birth Male',]

new_born['asphyxia'] = new_born[asphyxia].sum(axis=1)
successful_resuscitation = ['Babies not breathing/not crying at birth successfully resuscitated Female',
                             'Babies not breathing/not crying at birth successfully resuscitated Male']

new_born['successful_resuscitation'] = new_born[successful_resuscitation].sum(axis=1)
Validation Rules
conditions = [new_born['total_CHx'] == 0]
choices = ['Use of chlohexidine gel was not reported']
new_born['L&D_Remarks-1'] = np.select(conditions,choices, default='')
conditions = [new_born['skin_skin_warm']  == 0]
choices = ['Baby put skin_skin to keep warm was not reported']
new_born['L&D_Remarks-2'] = np.select(conditions,choices, default='')                  
conditions = [new_born['livebirths'] == 0]
choices = ['']
new_born['L&D_Remarks-3'] = np.select(conditions,choices, default='')               
conditions = [new_born['stillbirths'] > 0]
choices    = ['stillbirths was reported, verify!']
new_born['L&D_Remarks-4'] = np.select(conditions,choices, default='')
conditions = [new_born['total_CHx'] > new_born['livebirths']]
choices = ['Chlorohexidine gel reported is more than reported livebirth']
new_born['L&D_Remarks-5'] = np.select(conditions,choices, default='')             
conditions = [new_born['skin_skin_warm'] > new_born['livebirths']]
choices = ['Baby put skin_skin_warm is more than livebirths']
new_born['L&D_Remarks-6'] = np.select(conditions,choices, default='')             
conditions = [new_born['stillbirths']  > new_born['livebirths']]
choices = ['stillbirths is more than livebirths']
new_born['L&D_Remarks-7'] = np.select(conditions,choices, default='')             
conditions = [new_born['asphyxia'] > new_born['successful_resuscitation']]
choices = ['not all asphyxia cases was resuscitated, check for stillbirth']
new_born['L&D_Remarks-8'] = np.select(conditions,choices, default='')
new_born['unsuccessful_resuscitation']= new_born['successful_resuscitation'] - new_born['asphyxia']
conditions = [new_born['unsuccessful_resuscitation'] > new_born['stillbirths']]
choices = ['not all stillbirths were accounted from asphyxia cases']
new_born['L&D_Remarks-9'] = np.select(conditions,choices, default='')
Newborn
new_born2 = new_born[['Facility Name','LGA', 'Ward', 'periodname',
                       'L&D_Remarks-1', 'L&D_Remarks-2', 'L&D_Remarks-3', 'L&D_Remarks-4', 'L&D_Remarks-5',
                       'L&D_Remarks-6', 'L&D_Remarks-7', 'L&D_Remarks-8', 'L&D_Remarks-9']]
melt = new_born2.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/inb_remarks_all.xlsx', index = False)
 
Malaria
total_fever_cases = ['Persons with fever  ≥5yrs (excl PW)','Persons with fever <5yrs','Persons with fever Preg Women (PW)']
# adding summed columns to a dataframe
data['total_fever_cases'] = data[total_fever_cases].sum(axis=1)                    
test_by_RDT = ['Persons presenting with fever & tested by RDT  ≥5yrs (excl PW)',
               'Persons presenting with fever & tested by RDT <5yrs',
               'Persons presenting with fever & tested by RDT Preg Women (PW)']

# adding summed columns to a dataframe

data['test_by_RDT'] = data[test_by_RDT].sum(axis=1)                   
# Summing up columns

confirmed_uncomplicated = ['Persons with confirmed uncomplicated Malaria  ≥5yrs (excl PW)',
                            'Persons with confirmed uncomplicated Malaria <5yrs',
                            'Persons with confirmed uncomplicated Malaria Preg Women (PW)']

# adding summed columns to a dataframe

data['confirmed_uncomplicated'] = data[confirmed_uncomplicated].sum(axis=1)       
uncomp_malaria_treated_with_ACT = ['Persons with Confirmed Uncomplicated Malaria treated with ACT  ≥5yrs (excl PW)',
                                   'Persons with Confirmed Uncomplicated Malaria treated with ACT <5yrs',
                                   'Persons with Confirmed Uncomplicated Malaria treated with ACT Preg Women (PW)']

# adding summed columns to a dataframe

data['uncomp_malaria_treated_with_ACT'] = data[uncomp_malaria_treated_with_ACT].sum(axis=1) 
TotalTreatmentConfirmedUncomplicated = ['uncomp_malaria_treated_with_ACT',
                                        'Persons with Confirmed Uncomplicated Malaria treated with other antimalarials']

# adding summed columns to a dataframe

data['TotalTreatmentConfirmedUncomplicated'] = data[TotalTreatmentConfirmedUncomplicated].sum(axis=1)       
Totals
malaria = data[['Facility Name', 'LGA', 'Ward', 'periodname','total_fever_cases', 'test_by_RDT',
                'Persons tested positive for malaria by RDT','confirmed_uncomplicated',
                'uncomp_malaria_treated_with_ACT', 'TotalTreatmentConfirmedUncomplicated',
                'Persons with Confirmed Uncomplicated Malaria treated with other antimalarials']]
Conditions
conditions = [(malaria['total_fever_cases'] > malaria['test_by_RDT'])]                                            
choices = ['RDT test conducted are more than fever cases']
malaria['malaria_Remarks-1'] = np.select(conditions,choices, default='')         
conditions = [(malaria['Persons tested positive for malaria by RDT'] < malaria['confirmed_uncomplicated'])]                                            
choices = ['Confirmed Uncomplicated Malaria is more than postive RDT reported']
malaria['malaria_Remarks-2'] = np.select(conditions,choices, default='')         
conditions = [(malaria['TotalTreatmentConfirmedUncomplicated'] > malaria['confirmed_uncomplicated'])]                                            
choices = ['Treatment is more than confirmed uncomplicated malaria']
malaria['malaria_Remarks-3'] = np.select(conditions,choices, default='')         
conditions = [ malaria['total_fever_cases']  == 0]                                                
choices = ['no fever cases reported']
malaria['malaria_Remarks-4'] = np.select(conditions,choices, default='')         
Remarks Section
malaria= malaria[['Facility Name', 'LGA', 'Ward', 'periodname',
                  'malaria_Remarks-1','malaria_Remarks-2','malaria_Remarks-3',
                  'malaria_Remarks-4']]
melt = malaria.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/malaria_remarks_all.xls', index = False)
 
Uterotonics
importing libraries
mnh = data[['Facility Name','LGA','Ward',
            'periodname',
            'Deliveries Assisted',
            'Deliveries Caeserean Section',
            'Deliveries Spontaneous Vaginal Delivery (SVD)',
            'Women given Uterotonics in the 3rd stage of labour Oxytocin',
            'Women given Uterotonics in the 3rd stage of labour Misoprostol']]
utero = ['Women given Uterotonics in the 3rd stage of labour Oxytocin',
         'Women given Uterotonics in the 3rd stage of labour Misoprostol']

mnh['total_uterotonics'] = mnh[utero].sum(axis=1)   
deliveries_all = ['Deliveries Caeserean Section',
                  'Deliveries Assisted',
                  'Deliveries Spontaneous Vaginal Delivery (SVD)']

mnh['deliveries_all'] = mnh[deliveries_all].sum(axis=1)
Validation
#Creating a remarks columns 

conditions = [mnh['Women given Uterotonics in the 3rd stage of labour Oxytocin'] >  
              mnh['Deliveries Spontaneous Vaginal Delivery (SVD)']]

choices =  ['Oxytocin reported is more than total deliveries']

mnh['L&D_Remarks-1'] = np.select(conditions,choices, default = '')
#Creating a remarks columns 

conditions = [mnh['Women given Uterotonics in the 3rd stage of labour Oxytocin'] <  
              mnh['Deliveries Spontaneous Vaginal Delivery (SVD)']]

choices =  ['Oxytocin reported is less than total deliveries']

mnh['L&D_Remarks-2'] = np.select(conditions,choices, default = '')
#Creating a remarks columns 

conditions = [mnh['total_uterotonics'] == 0]

choices =  ['no reports of uterotonics']

mnh['L&D_Remarks-3'] = np.select(conditions,choices, default = '')
mnh = mnh[['Facility Name', 'LGA', 'Ward', 'periodname','L&D_Remarks-1', 'L&D_Remarks-2', 'L&D_Remarks-3']]
melt = mnh.melt(id_vars=['Facility Name','LGA', 'Ward', 'periodname'])
melt = melt.drop(index=melt[melt['value']==''].index)
melt.to_excel('output/uterotonics_remarks_all.xls', index = False)
 
 
Run cancelled
# How to import csv files into pandas Dataframe
#Import pandas as pd
import pandas as pd

# Import the cars.csv data: cars
cars = pd.read_csv('cars.csv')

# Print out cars
print(cars)
Run cancelled
# How to print out data in series and Dataframe using square brackets
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)

# Print out country column as Pandas Series
print(cars['country'])

# Print out country column as Pandas DataFrame
print(cars[['country']])

# Print out DataFrame with country and drives_right columns
print(cars[['country', 'drives_right']])
Run cancelled
# Define variables
my_kitchen = 18.0
your_kitchen = 14.0

# my_kitchen bigger than 10 and smaller than 18?
print(my_kitchen > 10 and my_kitchen < 18)

# my_kitchen smaller than 14 or bigger than 17?
print(my_kitchen < 14 or my_kitchen > 17)

# Double my_kitchen smaller than triple your_kitchen?
print(my_kitchen * 2 < your_kitchen * 3)
# Import pandas library
import pandas as pd

# Read the excel file
data = pd.read_excel('data.xlsx')

# Display the data
data.head()