<jupyter_start><jupyter_text>Columns of interest  Mom age C  Mom’s age to first baby C  Weeks born early starting at 35-40 M  Avg BirthWeights M  Multiple Births C  Plurality C  Method of Birth M  Birth Place M  BMI / Mom’s weight / weight gain M&C<jupyter_code>colnames = np.array(births.columns)
colnames<jupyter_output><empty_output><jupyter_text>Baby weight time series<jupyter_code>babywt = births[['birth_year', 'birth_month','birth_time','birth_weight_gm']]

meanwt_ts = babywt.groupby(['birth_year','birth_month'])['birth_weight_gm'].mean()<jupyter_output><empty_output><jupyter_text>Monthly Mean and Median birth weight by year<jupyter_code>plt.figure(figsize=(16,16))

meanwt_ts.plot()
# plt.xlabel('Number of cases of Plurality')
# plt.ylabel('Years based on amount of Twins')
medwt_ts = babywt.groupby(['birth_year','birth_month'])['birth_weight_gm'].median()
plt.figure(figsize=(16,16))
medwt_ts.plot()<jupyter_output><empty_output><jupyter_text>Yearly Mean and Median Birth weight<jupyter_code>meanwt_ts2 = babywt.groupby(['birth_year'])['birth_weight_gm'].mean()

plt.figure(figsize=(16,16))
meanwt_ts2.plot()
medwt_ts2 = babywt.groupby(['birth_year'])['birth_weight_gm'].median()

plt.figure(figsize=(16,16))
medwt_ts2.plot()<jupyter_output><empty_output><jupyter_text>Monthly variation in baby birth weight<jupyter_code>meanwt_ts3 = babywt.groupby(['birth_month'])['birth_weight_gm'].mean()

plt.figure(figsize=(16,16))
meanwt_ts3.plot()
medwt_ts3 = babywt.groupby(['birth_month'])['birth_weight_gm'].median()

plt.figure(figsize=(16,16))
medwt_ts3.plot()<jupyter_output><empty_output><jupyter_text>Birth method<jupyter_code>method  = (births[['birth_year', 'birth_month','final_delivery_method']])
# method.groupby('birth_year')

#Missingness
print(method['final_delivery_method'].value_counts())  #total missing 
print(method.groupby(['birth_year'])['final_delivery_method'].value_counts()) #missing per year<jupyter_output><empty_output><jupyter_text>Column cleaning  Add to general cleaning file<jupyter_code>method2 = method.mask(method['final_delivery_method'] == ' ')
# method
# print('Masked method \n',method2['final_delivery_method'].value_counts())
# print('Unmasked method \n', method['final_delivery_method'].value_counts())
# births['final_delivery_method'] = pd.to_numeric(births['final_delivery_method'])
method2['final_delivery_method'] = method2['final_delivery_method'].astype(float)
method2.dtypes
method2.groupby(['birth_year'])['final_delivery_method'].value_counts()
methodplt = method2.groupby(['birth_year'])['final_delivery_method'].value_counts()
methodplt.unstack().plot(logy = True, legend = False)
methodplt = method2.groupby(['birth_month'])['final_delivery_method'].value_counts()
methodplt.unstack().plot(logy = True, legend = False)
methodplt = method2.groupby(['birth_year','birth_month'])['final_delivery_method'].value_counts()
methodplt.unstack().plot(logy = True, legend = False)<jupyter_output><empty_output><jupyter_text>Place of birth trend<jupyter_code>place = (births[['birth_year', 'birth_month','birth_place']])
print(place['birth_place'].value_counts()) #289573 missing
print(place.groupby(['birth_year'])['birth_place'].value_counts()) #missing per year
place2 = place.mask(place['birth_place'] == ' ')
# place2
# print('Masked place \n', place2['birth_place'].value_counts())
place2['birth_place'] = place2['birth_place'].astype(float)
# method2.dtypes
place2.groupby(['birth_year'])['birth_place'].value_counts()
placeplt = place2.groupby(['birth_year'])['birth_place'].value_counts()
placeplt.unstack().plot(logy = True, legend = False)<jupyter_output><empty_output><jupyter_text>NICU admissions<jupyter_code>val_check(births, 'admit_NICU')
births['mothers_age_groups']= pd.cut(births['mothers_age'], 4, labels = ['12-19', '20-29', '30-39','40-50'])
births['admit_NICU'] = births['admit_NICU'].replace(' ', 'U')
births.groupby('birth_year')['admit_NICU'].value_counts().plot(kind='bar')
#births.mask(births['admit_NICU'] == 'U')
plot_gpYR(births.mask(births['admit_NICU'] == 'U'),'admit_NICU',"Year","Count of NICU admission", "Yearly NICU admittance",kind='line')

births2 = births.mask(births['admit_NICU'] == 'U')


births['admit_NICU'].value_counts().plot(kind = 'pie', autopct='%1.1f%%', pctdistance = 1.4)

test = births.loc[births['birth_year']==2014]
test['admit_NICU'].value_counts().plot(kind = 'pie', autopct='%1.2f%%', pctdistance = 1.4)
test = births.loc[births['birth_year']==2018]
test['admit_NICU'].value_counts().plot(kind = 'pie', autopct='%1.2f%%', pctdistance = 1.4)<jupyter_output><empty_output><jupyter_text>Mother health factors BMI and AGE<jupyter_code>#data cleaning
births['mothers_bmi'] = pd.to_numeric(births['mothers_bmi'], errors = 'coerce')
births['mother_bmi_recode'] = pd.cut(births['mothers_bmi'],[10.0, 19.0, 25.0,30.0,35.0,40.0,90.0], right = False)

births['mother_bmirecode']
# val_check(births,'mother_bmirecode')
births['mother_bmi_recode']
bmi_plt = births.groupby(['mothers_bmi_recode'])['admit_NICU'].value_counts().unstack()
bmi_plt.plot(kind = 'bar', stacked = True)
bmi_plt = births.groupby(['mother_bmirecode'])['admit_NICU'].value_counts().unstack()
bmi_plt.plot(kind = 'bar', stacked = True)
pct_bplot(births,'mother_bmi_recode')

val_check()
testvar = births.groupby(['mothers_bmi_recode'])[['admit_NICU']].count()

var2 =  births.groupby(['mothers_bmi_recode'])['admit_NICU'].value_counts().unstack()
var2 = var2.reindex(columns=columns_titles)
print(var2)
# print(type(testvar))
# print(testvar)
test_df3 = pd.merge(testvar,var2, left_index = True, right_index = True)
test_df3
def pct_bplot(dataframe, group = str, target = 'admit_NICU', column_title = ['Y','N'] ):
    df1 = dataframe.groupby([group])[[target]].count()
    df2 =  dataframe.groupby([group])[target].value_counts().unstack()
    df2 = df2.reindex(columns=column_title)
    df3 = pd.merge(df1,df2, left_index = True, right_index = True)
    pct_df = pd.DataFrame(list(map(lambda x: df3[x]/df3[target] * 100, df3.columns[1:])))
    pct_df = pct_df.T
    pct_df.plot(kind = 'bar', stacked = True)
births.columns
pct_bplot(births,'pre_preg_diab')

pct_bplot(births,'gest_diab')
#Example code

df2 = df.groupby(['Name', 'Abuse/NFF'])['Name'].count().unstack('Abuse/NFF').fillna(0)
df2[['abuse','nff']].plot(kind='bar', stacked=True,autopct='%1.2f%%')

#percetnage barplot
sns.barplot(x='group', y='Values', data=df, estimator=lambda x: sum(x==0)*100.0/len(x))

totals = [i+j+k for i,j,k in zip(df['pre_score'], df['mid_score'], df['post_score'])]

# Create the percentage of the total score the pre_score value for each participant was
pre_rel = [i / j * 100 for  i,j in zip(df['pre_score'], totals)]
plot_gpYR(births,'mothers_bmi_recode','year','BMI', 'BMI trend')<jupyter_output><empty_output><jupyter_text>Testing helper functions<jupyter_code>def val_check(data_frame, column_name = str):
    df = pd.DataFrame(data_frame)
    col = column_name
    print( "Value counts of %s \n" %(col), df[col].value_counts())
    print("Value counts of %s by year \n" %(col), df.groupby(['birth_year'])[col].value_counts())

def plotter(col):
    df[col].groupby(df['year'])[col].value.counts()
    col.plot(logy=True, legend = False
    xlabel
    ylabel
             
def map_plot(df):
    map(lamba col: plotter(col), col in df)
def plot_gpYR(df,col,xlab,ylab,title,kind='line'):
    groupDF = df.groupby('birth_year')[col].value_counts().unstack()
    plt.figure(figsize=(20,20))
    groupDF.plot(kind=kind,logy=True, legend=True)
    plt.xlabel(xlab)
    plt.ylabel(ylab)
    plt.title(title)
    return
def pct_bplot(dataframe, group = str, target = 'admit_NICU', column_title = ['Y','N'] ):
    df1 = dataframe.groupby([group])[[target]].count()
    df2 =  dataframe.groupby([group])[target].value_counts().unstack()
    df2 = df2.reindex(columns=columns_titles)
    df3 = pd.merge(df1,df2, left_index = True, right_index = True)
    pct_df = pd.DataFrame(list(map(lambda x: df3[x]/df3[target] * 100, df3.columns[1:])))
    pct_df = pct_df.T
    test_df2.plot(kind = 'bar', stacked = True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17952833683170763, 'fraction_numerical': 0.021113717742931228, 'mean_word_length': 7.940397350993377, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '201158', 'license': 'FTL', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3084, 'n_words': 439, 'path': 'notebooks/MK_EDA_timeseries.ipynb', 'repo_name': 'ConnorHaas03/CDC_capstone', 'size': '8099'}