<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Todo
Step1:  Rel risk. P(gender|misaligned)/P(gender)
What proportion of the misaligned dataset is about women?
For each gender, what proportion of the each misalignment group do the represent.
<ASSISTANT_TASK:>
Python Code:
bigdf = pandas.read_csv('/media/notconfusing/9d9b45fc-55f7-428c-a228-1c4c4a1b728c/home/maximilianklein/snapshot_data/2016-01-03/gender-index-data-2016-01-03.csv')
gender_qid_df = bigdf[['qid','gender']]
def map_gender(x):
    if isinstance(x,float):
        return 'no gender'
    else:
        gen = x.split('|')[0]
        if gen == 'Q6581072':
            return 'female'
        elif gen == 'Q6581097':
            return 'male'
        else:
            return 'nonbin'
gender_qid_df['gender'] = gender_qid_df['gender'].apply(map_gender)
def qid2enname(x):
    try:
        return qidnames[x]
    except KeyError:
        return None
gender_qid_df['enname'] = gender_qid_df['qid'].apply(qid2enname)
enname_id = pandas.read_csv('/home/notconfusing/workspace/wikidumpparse/wikidump/mediawiki-utilities/enname_id.txt',sep='\t',names=['enname','pageid'])
gender_page_id = pandas.merge(gender_qid_df, enname_id, how='inner',on='enname')
pah_gender = pandas.merge(pah, gender_page_id, how='left', on='pageid')
pah_gender
len(pah), len(gender_page_id), len(pah_gender)
Explanation: Todo:
    + map gender-enwiki-page-id
    + dissoance class priors
    + posteriors by gender
    + posterior for no gender.
End of explanation
pah_gender['gender'] = pah_gender['gender'].fillna('nonbio')
SE = pah_gender[(pah_gender['dissonance'] == 'Moderate negative') | (pah_gender['dissonance'] == 'High negative')]
NI = pah_gender[(pah_gender['dissonance'] == 'Moderate positive') | (pah_gender['dissonance'] == 'High positive')]
rel_risk = defaultdict(dict)
for risk, risk_name in [(SE,'Spent Effort'), (NI,'Needs Improvement')]:
    for gender in ['female','male','nonbin','nonbio']:
        gen_mis = len(risk[risk['gender'] == gender])
        p_gen_mis = gen_mis/len(risk)               #p(gender|misalignment)
        p_gen = len(pah_gender[pah_gender['gender'] == gender]) / len(pah_gender)   #p(gender)
        print(p_gen_mis, p_gen)
        rel_risk[gender][risk_name] = p_gen_mis/p_gen#rel sirk
    java_min_int = -2147483648
allrecs = pandas.read_csv('/media/notconfusing/9d9b45fc-55f7-428c-a228-1c4c4a1b728c/home/maximilianklein/snapshot_data/2016-01-03/gender-index-data-2016-01-03.csv',na_values=[java_min_int])
def sum_column(q_str):
    if type(q_str) is str:
        qs = q_str.split('|')
        return len(qs) #cos the format will always end with a |
for col in ['site_links']:
    allrecs[col] = allrecs[col].apply(sum_column)
allrecs['site_links'].head(20)
allrecs['gender'] = allrecs['gender'].apply(map_gender)
sl_risk = defaultdict(dict)
sl_risk['nonbio']['Sitelink Ratio'] = 1
for gender in ['female','male','nonbin']:
    gend_df = allrecs[allrecs['gender']==gender]
    gend_df_size = len(gend_df)
    avg_sl = (gend_df['site_links'].sum() / gend_df_size)  / 2.6
    sl_risk[gender]['Sitelink Ratio'] = avg_sl
sl_risk_df = pandas.DataFrame.from_dict(sl_risk, orient='index')
rel_risk_df = pandas.DataFrame.from_dict(rel_risk,orient="index")
risk_df = pandas.DataFrame.join(sl_risk_df,rel_risk_df)
risk_df.index = ['Female','Male','Non-binary','Non-biography']
print(risk_df.to_latex(columns = ['Needs Improvement','Spent Effort', 'Sitelink Ratio'],float_format=lambda n:'%.2f' %n))
Explanation: Rel risk. P(gender|misaligned)/P(gender)
What proportion of the misaligned dataset is about women?
For each gender, what proportion of the each misalignment group do the represent.
End of explanation
<END_TASK>