Painting a picture with the numbers
The code
- import pandas as pd
- import numpy as np
- import seaborn
- import matplotlib.pyplot as plt
- plt.rcdefaults()
- #show max rows and columns in dataframe with the following two lines
- pd.set_option('display.max_columns', None)
- pd.set_option('display.max_rows', None)
- pd.options.mode.chained_assignment = None
- #to prevent runtime errors use the following code
- pd.set_option('display.float_format', lambda x: '%f'%x)
- #filename = 'C:\\Users\\Christopher\\OneDrive for Business\\Data studies\\addhealth_pds.csv'
- filename ='addhealth_pds.csv'
- #create a dataset from original csv file
- #mydata = pd.read_csv('addhealth_pds.csv', low_memory=False)
- mydata = pd.read_csv(filename, low_memory=False)
- #ensure all data column headers are uppercase with the following
- mydata.columns = map(str.upper, mydata.columns)
- #convert columns to numeric rather than data
- pd.to_numeric(mydata['H1ED5']) #failed any grade
- pd.to_numeric(mydata["H1ED6A"])#failed 1ST grade
- pd.to_numeric(mydata["H1ED6B"]) #failed 2ND grade
- pd.to_numeric(mydata["H1ED6C"]) #failed 3RD grade
- pd.to_numeric(mydata["H1ED6D"]) #failed 4th grade
- pd.to_numeric(mydata["H1ED6E"])#failed 5th grade
- pd.to_numeric(mydata["H1ED6F"])#failed 6th grade
- pd.to_numeric(mydata["H1ED6G"]) #failed 7th grade
- pd.to_numeric(mydata["H1ED6H"]) #failed 8th grade
- pd.to_numeric(mydata["H1ED6I"]) #failed 9th grade
- pd.to_numeric(mydata["H1ED6J"]) #failed 10th grade
- pd.to_numeric(mydata["H1ED6K"]) #failed 11th grade
- pd.to_numeric(mydata["H1FV5"]) #got into fight
- pd.to_numeric(mydata["H1FV7"]) #pulled a knife or gun
- pd.to_numeric(mydata["H1FV8"]) #shot or stabbed someone
- mydata['H1ED5']=mydata['H1ED5'].replace(6, np.nan)
- mydata['H1ED5']=mydata['H1ED5'].replace(8, np.nan)
- def retained (row):
- if row["NUMRETAINED"] > 1 and row['NUMRETAINED'] <=3:
- return 13
- if row["NUMRETAINED"] > 3:
- #return np.nan
- return 0
- if row['H1ED6A'] == 1:
- return 1
- if row['H1ED6B'] == 1:
- return 2
- if row['H1ED6C'] == 1:
- return 3
- if row['H1ED6D'] == 1:
- return 4
- if row['H1ED6E'] == 1:
- return 5
- if row['H1ED6F'] == 1:
- return 6
- if row['H1ED6G'] == 1:
- return 7
- if row['H1ED6H'] == 1:
- return 8
- if row['H1ED6I'] == 1:
- return 9
- if row['H1ED6J'] == 1:
- return 10
- if row['H1ED6K'] == 1:
- return 11
- else:
- return 0
- sub1 = mydata[(mydata['H1ED5'] >= 0) & (mydata['H1ED5'] <= 1)]
- sub2 = sub1.copy
- #This line uses the above function to fill in the grade level the respondent
- # was retained
- sub1["NUMRETAINED"] = (0 + sub1['H1ED6A'] + sub1['H1ED6B'] + sub1['H1ED6C'] +
- sub1['H1ED6D'] + sub1['H1ED6E'] + sub1['H1ED6F'] + sub1['H1ED6G'] +
- sub1['H1ED6H'] + sub1['H1ED6I'] + sub1['H1ED6J'] + sub1['H1ED6K'])
- sub1['GRADERETAINED'] = sub1.apply(lambda row: retained (row), axis = 1)
- sub1['GRADELEVEL']=pd.cut(sub1.GRADERETAINED,[0, 5, 9, 12, 13])
- def violence (row):
- if row ["H1FV5"] >=1 and row ["H1FV5"] <=2:
- return 1
- if row ["H1FV7"] >=1 and row ["H1FV7"] <=2:
- return 1
- if row ["H1FV8"] >=1 and row ["H1FV8"] <=2:
- return 1
- else:
- return 0
- sub1['VIOLENT'] = sub1.apply(lambda row: violence (row), axis = 1)
- delinquent = sub1["VIOLENT"].value_counts(sort=True, dropna=False)
- print("Count of respondents who fought and/or used a weapon\n")
- print("\t 0= Have not done so and 1 = have fought or used a weapon")
- print(delinquent)
- seaborn.countplot(x='VIOLENT', data=sub1)
- plt.xlabel('0- No violence, 1- has used violence')
- plt.title("In last 12 months have you fought and/or used a weapon against \
- another?")
- seaborn.factorplot(x="GRADERETAINED", y="VIOLENT", data=sub1, kind="point", ci=None)
- plt.xlabel('Grade Level')
- plt.ylabel('Percent of violent repeaters')
- desc1 = sub1["GRADERETAINED"].value_counts(sort=True, dropna=True)
- print(desc1)
- seaborn.factorplot(x="GRADELEVEL", y="VIOLENT", data=sub1, kind="bar", ci=None)
- plt.xlabel('Grade Level')
- plt.ylabel('Percent of violent repeaters')
- seaborn.countplot(x="GRADELEVEL", data=sub1)
- plt.xlabel("Grade level retained")
- plt.ylabel("Number retained")
- plt.title("Count of students retained Elementary, Middle, High, Multiple")
- seaborn.factorplot(x="H1ED5", y="VIOLENT", data=sub1, kind="bar")
- plt.xlabel("0=Never Retained, 1 = retained at least one grade")
- plt.ylabel("Percent who have been violent")
The graphs
This univariate graph shows the number of students retained in each grade level. 0-5 is Elementary, 5-9 is Middle School, 9-12 is high school and 12-13 are students who were retained in multiple grade levels. There is a clear trend toward fewer retentions the higher the level.
This univariate graph shows more than 2/3 of respondents had not committed a violent act in the last 12 months approximately 4500 to 2000.
The above graph shows that just under 30% of students who were never retained had committed a violent act in the last 12 months while nearly 45% of students who had been retained at least once had committed a violent act.
Among respondents who had been retained, there is a clear positive relationship between the grade and the percentage of students who are violent. There is a clear indication that a strong correlation exists between grade retention from 5th grade on.
This is another view of the data binned into Elementary, Middle, High School and Multiple grade levels. The highest percentage of students who are violent occurs in high school while the lowest percent occurs in elementary. This is despite the fact that far more students are retained in elementary than in middle and high school.





No comments:
Post a Comment