Monday, November 28, 2016


# import modules to process math

import numpy
import pandas as pd

#to prevent runtime errors use the following code
pd.set_option('display.float_format', lambda x:'%f'%x)

#create a dataset from original csv file
data = pd.read_csv('addhealth_pds.csv', low_memory=False)
print("The number of rows")
print(len(data)) # prints the number of rows in dataset
print("The number of variables in the dataset")
print(len(data.columns)) # prints the number of variables

#ensure all data column headers are uppercase with the following
data.columns = map(str.upper, data.columns)

#convert column H1ED5 to numeric rather than data
#data['H1ED5'] = data['H1ED5'].convert_objects(convert_numeric = True)
pd.to_numeric(data['H1ED5'])
#data['H1ED6G'] = data['H1ED6G'].convert_objects(convert_numeric = True)
pd.to_numeric(data["H1ED6G"])
print("Count of each answer to 'Have you ever been held back or repeated a grade?'")
print("0='No', 1='Yes', 6='Refused' and 8='I don't know''")
repeat = data ["H1ED5"].value_counts(sort=False) #returns the data in selected columns
seventh = data['H1ED6G']
#below returns the percentage of each answer
percrepeat = data ["H1ED5"].value_counts(sort=False, normalize = True, dropna = False)
print("The data does not come out in numerical order of answer choices this way")
print (repeat)
print("The percentage of each answer choice - again not in numerical order")
print (percrepeat)
#another way to count the data below
print("Below are the counts using the 'data.groupby' function")
print("Count of each answer to 'Have you ever been held back or repeated a grade?'")
print("0='No', 1='Yes', 6='Refused' and 8='I don't know''")
ct1 = data.groupby('H1ED5').size()
print (ct1)
# to get the percentage multiply by 100 then divide by number of rows (len(Data))
print("Below are the percentages using the 'data.groupby' function")
print("Percentage of each answer to 'Have you ever been held back or repeated a grade?'")
print("0='No', 1='Yes', 6='Refused' and 8='I don't know''")
pt1 = data.groupby('H1ED5').size() * 100 / len(data)
print (pt1)

allheldback = data[(data['H1ED5']==1)]
#select only rows from the column that meet a specific criteria below shows
# only those in 7th grade who answered "yes" to being held back

retain = data[(data['H1ED5']==1) & (data['H1ED6G']==1)]
retain1 = retain.copy() #make a copy of the subset
#create variable for answers given to Were you held back in 7th grade
print("0= held back but not in 7th, 1= held back in 7th, 7= legitimate skip 8 = Don't know")
#seven = data['H1ED6G'].value_counts(sort = True, dropna = True)
seven = allheldback.groupby('H1ED6G').size()
print (seven)
yes7 = retain.groupby('H1ED5').size() * 100 / len(allheldback)
print('The percentage of all students who reported being held back who repeated 7th grade')
print (yes7)
fight7 = retain.groupby('H1FV5').size()
print('Answers from students held back in 7th grade to "How often were you in a fight in the last 12 months"')
print('0="Never", 1 = "Once", 2= "More than once", 6="Refused"')
print(fight7)
pull7 = retain.groupby('H1FV7').size()
print('Answers from students held back in 7th grade to "How often did you pull a knife or gun on someone in the last 12 months"')
print('0="Never", 1 = "Once", 2= "More than once", 6="Refused"')
print(pull7)
shoot7 = retain.groupby('H1FV8').size()
print('Answers from students held back in 7th grade to "How often did you shoot or stab someone in the last 12 months"')
print('0="Never", 1 = "Once", 2= "More than once", 6="Refused"')
print(shoot7)







No comments:

Post a Comment