Assignment: Making Data Management Decisions
1. My program
import pandas as pd import numpy as nu
# bug fix for display formats to avoid run time errors - put after code for loading data above pd.set_option(‘display.float_format’, lambda x:’%f’%x)
data = pd.read_csv('nesarc_pds.csv', low_memory=False)
#upper-case all DataFrame column names data.columns = map(str.upper, data.columns)
#setting variables you will be working with to numeric
data['S1Q2D'] = data['S1Q2D'].convert_objects(convert_numeric=True) data['S1Q2E'] = data['S1Q2E'].convert_objects(convert_numeric=True) data['S1Q2L'] = data['S1Q2L'].convert_objects(convert_numeric=True) data['S1Q2K'] = data['S1Q2K'].convert_objects(convert_numeric=True) data['S6Q1'] = data['S6Q1'].convert_objects(convert_numeric=True)
#subset data subdiv=data[(data['S1Q2E']>=3) & (data['S1Q2E']<=12) & (data['S1Q2D']==1)] subdie=data[(data['S1Q2L']>=3) & (data['S1Q2L']<=12) & (data['S1Q2K']==1)] #make a copy of my new subsetted data subdiv2 = subdiv.copy() subdie2 = subdie.copy() data2 = data.copy()
print ('counts for original S6Q1 (panic attack)') print(data2['S1Q2K'].value_counts(sort=False, dropna=False))
# recode missing values to python missing (NaN) data2['S6Q1'] = data2['S6Q1'].replace(9, nu.nan)
#if you want to include a count of missing add ,dropna=False after sort=False print ('counts for S6Q1 (panic attack) with 9 set to NAN and number of missing requested') print(data2['S6Q1'].value_counts(sort=False, dropna=False))
# categorize quantitative variable based on customized splits using cut function # splits into 3 groups (3-5, 6-9, 10-12) subdiv2['AGEDIV'] = pd.cut(subdiv2.S1Q2E, [2, 5, 9, 12]) subdie2['AGEDIE'] = pd.cut(subdie2.S1Q2L, [2, 5, 9, 12]) print(subdiv2['AGEDIV'].value_counts(sort=False, dropna=True)) print(subdie2['AGEDIE'].value_counts(sort=False, dropna=True))
#crosstabs evaluating which ages were put into which AGEGROUP3 print (pd.crosstab(subdiv2['AGEDIV'], subdiv2['S1Q2E'])) print (pd.crosstab(subdie2['AGEDIE'], subdie2['S1Q2L']))
#frequency distribution for AGEGROUP3 print ('counts for AGEDIV and AGEDIE') print(subdiv2['AGEDIV'].value_counts(sort=False)) print (subdie2['AGEDIE'].value_counts(sort=False))
print ('percentages for AGEDIV and AGEDIE') print(subdiv2['AGEDIV'].value_counts(sort=False, normalize=True)) print (subdie2['AGEDIE'].value_counts(sort=False, normalize=True))
2. The output that displays three of my variables as frequency tables + description comments
Counts for original S6Q1 (panic attack)
NaN 1036
1 4515
2 37358
9 184 (184 undefined responds)
Counts for S6Q1 (panic attack) with 9 set to NAN and number of missing requested NaN 1386 (undefined responds www replaced by NANs) 1 5278 2 36429 Cross tabs evaluating which ages were put into which age group S1Q2E 3 4 5 6 7 8 9 10 11 12 AGEDIV [2, 5] 420 374 562 0 0 0 0 0 0 0 [5, 9] 0 0 0 406 388 422 335 0 0 0 [9, 12] 0 0 0 0 0 0 0 478 332 538
S1Q2L 3 4 5 6 7 8 9 10 11 12 AGEDIE [2, 5] 218 177 225 0 0 0 0 0 0 0 [5, 9] 0 0 0 206 243 218 249 0 0 0 [9, 12] 0 0 0 0 0 0 0 251 227 337
Counts for AGEDIV and AGEDIE [2, 5] 1356 [5, 9] 1551 [9, 12] 1348 dtype: int64 [2, 5] 620 [5, 9] 916 [9, 12] 815 dtype: int64 Percentages for AGEDIV and AGEDIE [2, 5] 0.318684 [5, 9] 0.364512 [9, 12] 0.316804 dtype: float64 [2, 5] 0.263718 [5, 9] 0.389621 [9, 12] 0.346661 type: float64
I grouped ages into 3 groups: 3-5, 6-9 and 10-12 y.o. Among those whose parents got divorced:
- for 3-5 y.o. the frequency was 1356 (31.86%)
- for 6-9 y.o. the frequency was 1551 (36.451%)
- for 10-12 y.o. the frequency was 1348 (31.68%)
Meaning that divorces in families with children in all three age groups are evenly distributed.
Among those whose parents died:
- for 3-5 y.o. the frequency was 620 (26.37%)
- for 6-9 y.o. the frequency was 916 (38.96%)
- for 10-12 y.o. the frequency was 815 (34.66%)
Meaning that 3-5 y.o. children whose parents died underrepresented with about 10% difference in our sample.












