<jupyter_start><jupyter_text>1. Graph showing mean, min and max age of MPs for each office<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Import 'combined_parliaments_new.csv' file from local directory<jupyter_code>import os
os.chdir('C:/Users/Kuma/Documents/Python Scripts/FML')
cp_raw=pd.read_csv('combined_parliaments_new.csv')<jupyter_output><empty_output><jupyter_text>Subset parliament office number and corresponding year<jupyter_code>year_raw=cp_raw[['Year', 'Number']]
year_raw['Number'].astype(str)
year_raw['Number']=year_raw['Number'].str.extract('(\d+)')#delete "th" and "st" from Number column
year=year_raw.dropna()#delete nas
year.columns=['year', 'office']#rename columns
year=year.astype(int)#change to int
year<jupyter_output><empty_output><jupyter_text>Subset office and age columns<jupyter_code>cp=cp_raw[['Office', 'Age']]
cp
#count the number of NAs in each office
nas=cp.Age.isnull().groupby(cp['Office']).sum().astype(int).reset_index(name='age.is.na')
nas<jupyter_output><empty_output><jupyter_text>Calculate the mean, min, max age and count for each office<jupyter_code>calc=cp.groupby('Office').agg({'Age':['count','mean', 'min','max']}).astype(int)<jupyter_output><empty_output><jupyter_text>Summary of MP age data by office/year<jupyter_code>#merge the tables
result_raw=pd.merge(calc, nas, on='Office', how='left')
result_raw=result_raw.rename(columns={'Office':'office'})#rename column Office to office
#rename columns
result_raw.columns=['office', 'count', 'mean', 'min', 'max', 'age.is.na']
result=pd.merge(result_raw, year, on='office', how='left')

#calculate percentage of unknown age per office
result['percent.na']=result['age.is.na'] / result['count'] *100
result=result.round(1)#set decimal place to 1
result<jupyter_output><empty_output><jupyter_text>Create a lineplot showing the mean, min, max age by year<jupyter_code>#put mean, min, max into long form
result_long=pd.melt(result, id_vars=['office','count','age.is.na','year','percent.na'], value_vars=['mean','min', 'max'])
result_long

#plot a lineplot showing the mean,min,max age for each office year
sns.lineplot(data=result_long, x="year", y="value",hue="variable", marker="o")
plt.ylim(0,100)
plt.xlabel("Year")
plt.ylabel("Age")
plt.legend(bbox_to_anchor=(1.05,1), borderaxespad=0.)<jupyter_output><empty_output><jupyter_text>Same plot as above but showing by office instead of year<jupyter_code>#plot a lineplot showing the mean,min,max age for each office year
sns.lineplot(data=result_long, x="office", y="value",hue="variable", marker="o")
plt.ylim(0,100)
plt.xlabel("Office")
plt.ylabel("Age")
plt.legend(bbox_to_anchor=(1.05,1), borderaxespad=0.)<jupyter_output><empty_output><jupyter_text>2. Percent make up of MPs by age groups<jupyter_code>cp.columns=['office', 'age']#rename cp columns
mpage=pd.merge(year, cp, on='office') #join df year and cp<jupyter_output><empty_output><jupyter_text>Creating the buckets<jupyter_code>#create a function to classify each age

def func(x):
    if x >= 70:
        return "Above 70"
    elif 70>x>=60:
        return "60_to_69"
    elif 60>x>=50:
        return "50_to_59"
    elif 50>x>=40:
        return "40_to_49"
    elif 40>x>=30:
        return "30_to_39"
    else:
        return "under_30"

mpage['classification']=mpage['age'].apply(func) #add classification column
mpage<jupyter_output><empty_output><jupyter_text>Calculate the percentage of age group in office<jupyter_code>count=mpage.groupby(['year', 'office', 'classification']).count().astype(int)#shown as percentage per age
per=count.groupby(level=0).apply(lambda x: 100 * x / float(x.sum())).reset_index()
per.rename(columns={'age':'percent'}, inplace=True)#rename age column to age_count
per<jupyter_output><empty_output><jupyter_text>Stacked barplot showing the proportion of MPs in each age bucket, bucket set to 10 year increments<jupyter_code>#stacked bar
bar1=sns.barplot(data=per, x='office', y='percent', hue='classification')
plt.legend(bbox_to_anchor=(1.05,1), borderaxespad=0.)

per_summary=per.pivot(index=['office', 'year'], columns='classification', values='percent')
per_summary
per_summary.to_csv(r')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15341830822711472, 'fraction_numerical': 0.01738122827346466, 'mean_word_length': 8.01043841336117, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 58, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '648377', 'license': 'MIT', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1530, 'n_words': 319, 'path': 'Momo_FML_parliament_age_graph.ipynb', 'repo_name': 'hirephd-org/FML2021', 'size': '4315'}