<jupyter_start><jupyter_text>Objective: MI, MACE, HF, CAD, stroke before that by any margin  Packages<jupyter_code>import pandas as pd 
import numpy as np 
import scipy.stats as stats
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Dataset<jupyter_code>#primary care
pri_cli = pd.read_csv('../primary_care/gp_clinical.txt', sep = '\t', encoding='ISO-8859-1')
#secondary care
sec_cli = pd.read_csv('../primary_care/ukb41199.csv', usecols=[0,
                          3239, 3240, 3241, 3242, 3243, 3244, 3245, 3246, 3247, 
                          3248, 3249, 3250, 8330, 8331, 8332, 8333, 8334, 8335,
                          8336, 8337, 8338, 8339, 8340, 8341, 8342, 8343, 8346])
total_patients = set(list(sec_cli.eid.unique()) + list(pri_cli.eid.unique()))
len(total_patients)
sec_pri_icd10= [0,] + list(range(6693, 6759))
sec_pri_icd9= [0,] + list(range(6759, 6787))
sec_sec_icd10= [0,] + list(range(6787, 6971))
sec_sec_icd9= [0,] + list(range(6971, 7001))
sec_pri10_diag = pd.read_csv('../primary_care/ukb41199.csv', usecols=sec_pri_icd10)
sec_pri9_diag = pd.read_csv('../primary_care/ukb41199.csv', usecols=sec_pri_icd9)
sec_sec10_diag = pd.read_csv('../primary_care/ukb41199.csv', usecols=sec_sec_icd10)
sec_sec9_diag = pd.read_csv('../primary_care/ukb41199.csv', usecols=sec_sec_icd9)
sec_pri10_date = pd.read_csv('../primary_care/ukb41199.csv', usecols=([0,] + list(range(7450,7516))))
sec_pri9_date = pd.read_csv('../primary_care/ukb41199.csv', usecols=([0,] + list(range(7516,7544))))
#demo
pri_demo = pd.read_csv('../primary_care/ukb41199.csv', usecols=[0,6])
#diag code
diag_codes = pd.read_excel('../primary_care/ProposedDiagnosisCodes.xlsx', sheet_name = "diag")
icd10_codes = pd.read_excel('../primary_care/ProposedDiagnosisCodes.xlsx', sheet_name = "icd10")
icd9_codes = pd.read_excel('../primary_care/ProposedDiagnosisCodes.xlsx', sheet_name = "icd9")

#drop duplicates 
diag_codes_summ = diag_codes[['READV2_CODE', 'READV3_CODE', 'Condition']].drop_duplicates(subset=None, keep='first', inplace=False)<jupyter_output><empty_output><jupyter_text>Diagnosis coding Pri<jupyter_code>disease = {
    'stroke' : {
        'read2': list(diag_codes[diag_codes['Condition']== 'Stroke']['READV2_CODE']),
        'read3': list(diag_codes[diag_codes['Condition']== 'Stroke']['READV3_CODE'])
    },
    'HF' : {
        'read2': list(diag_codes[diag_codes['Condition']== 'HF']['READV2_CODE']),
        'read3': list(diag_codes[diag_codes['Condition']== 'HF']['READV3_CODE'])
    }, 
    'CAD' : {
        'read2': list(diag_codes[diag_codes['Condition']== 'CAD']['READV2_CODE']),
        'read3': list(diag_codes[diag_codes['Condition']== 'CAD']['READV3_CODE'])
    },
    'MI' : {
        'read2': list(diag_codes[diag_codes['Condition']== 'MI']['READV2_CODE']),
        'read3': list(diag_codes[diag_codes['Condition']== 'MI']['READV3_CODE'])
    },
    'precursor' : {
        'read2': list(diag_codes[diag_codes['Condition']== 'precursor']['READV2_CODE']),
        'read3': list(diag_codes[diag_codes['Condition']== 'precursor']['READV3_CODE'])
    },
    'TIA' : {
        'read2': list(diag_codes[diag_codes['Condition']== 'TIA']['READV2_CODE']),
        'read3': list(diag_codes[diag_codes['Condition']== 'TIA']['READV3_CODE'])
    },
    'Afib' : {
        'read2': list(diag_codes[diag_codes['Condition']== 'Afib']['READV2_CODE']),
        'read3': list(diag_codes[diag_codes['Condition']== 'Afib']['READV3_CODE'])
    },
    'all' : {
        'read2': list(diag_codes['READV2_CODE'].values),
        'read3': list(diag_codes['READV3_CODE'].values)
    }
}
#isolating codes from primary care
diagnosed = pri_cli[pri_cli['read_2'].isin(disease['all']['read2'])| pri_cli['read_3'].isin(disease['all']['read3'])]
#adding condition codes
condition = []
for i in range(len(diagnosed['eid'])):
    if type(diagnosed.iloc[i]['read_2']) == str:
        key = diagnosed.iloc[i]['read_2']
        a = diag_codes_summ[diag_codes_summ['READV2_CODE'] == key]['Condition'].values[0]
        condition.append(a)
    elif type(diagnosed.iloc[i]['read_3']) == str: 
        key = diagnosed.iloc[i]['read_3']
        a = diag_codes_summ[diag_codes_summ['READV3_CODE'] == key]['Condition'].values[0]
        condition.append(a)
    else:
        condition.append(np.nan)
diagnosed['condition'] = condition
diagnosed
# remove duplicates
diagnosed.drop_duplicates(subset=None, keep='first', inplace=True)
# setting the list with (date, condition, 'pri')
pri_date_diag = []
for i in range(len(diagnosed['eid'])):
    a = [diagnosed.iloc[i]['event_dt'], diagnosed.iloc[i]['condition'].lower(), 'pri']
    pri_date_diag.append(a)
diagnosed['pri_date_diag'] = pri_date_diag
diagnosed
#order by id
pri_records = diagnosed.groupby('eid').agg(list).reset_index()
pri_records.iloc[21]['pri_date_diag']<jupyter_output><empty_output><jupyter_text>Secondary codes<jupyter_code>ICD = {
    10: {
        'stroke': list(icd10_codes[icd10_codes['diagnosis'] == 'stroke']['ICD10']),
        'tia': list(icd10_codes[icd10_codes['diagnosis'] == 'tia']['ICD10']),
        'haemorrhage': list(icd10_codes[icd10_codes['diagnosis'] == 'haemorrhage']['ICD10']),
        'cad': list(icd10_codes[icd10_codes['diagnosis'] == 'cad']['ICD10']),
        'mi': list(icd10_codes[icd10_codes['diagnosis'] == 'haemorrhage']['ICD10']),
        'hf': list(icd10_codes[icd10_codes['diagnosis'] == 'hf']['ICD10']),
        'afib': list(icd10_codes[icd10_codes['diagnosis'] == 'afib']['ICD10']),
        'all': list(icd10_codes['ICD10'])
    },
    9: {
        'stroke': list(icd9_codes[icd9_codes['diagnosis'] == 'stroke']['ICD9']),
        'tia': list(icd9_codes[icd9_codes['diagnosis'] == 'tia']['ICD9']),
        'haemorrhage': list(icd9_codes[icd9_codes['diagnosis'] == 'haemorrhage']['ICD9']),
        'cad': list(icd9_codes[icd9_codes['diagnosis'] == 'cad']['ICD9']),
        'mi': list(icd9_codes[icd9_codes['diagnosis'] == 'haemorrhage']['ICD9']),
        'hf': list(icd9_codes[icd9_codes['diagnosis'] == 'hf']['ICD9']),
        'afib': list(icd9_codes[icd9_codes['diagnosis'] == 'afib']['ICD9']),
        'all': list(icd9_codes['ICD9'])
    }
}
sec_pri10_date.columns = ['eid',] + list(map(lambda x: str(x), range(0,66)))
sec_pri10_diag.columns = ['eid',] + list(map(lambda x: str(x), range(0,66)))
sec_pri10 = pd.merge(sec_pri10_date, sec_pri10_diag, on= 'eid', how='inner')
sec_pri9_date.columns = ['eid',] + list(map(lambda x: str(x), range(0,28)))
sec_pri9_diag.columns = ['eid',] + list(map(lambda x: str(x), range(0,28)))
sec_pri9 = pd.merge(sec_pri9_date, sec_pri9_diag, on= 'eid', how='inner')
sec_pri10
sec_pri10.shape
def aggregate(df, name):
    cols = list(df.columns)[1:]
    collen = len(cols)
    result = []
    for i in range(df.shape[0]):
        subresult = []
        for x in cols: 
            temp = df.iloc[i][x]
            if type(x) == str:
                subresult.append(x)
    df[name] = result
    print('fin')
                
        
sec_pri10_date['combined'] = sec_pri10_date.values.tolist()
sec_pri9_date['combined'] = sec_pri9_date.values.tolist()
sec_pri10_diag['combined'] = sec_pri10_diag.values.tolist()
sec_pri9_diag['combined'] = sec_pri9_diag.values.tolist()
sec_pri10 = pd.merge(sec_pri10_date[['eid','combined']], sec_pri10_diag[['eid','combined']], on = 'eid', how = 'inner')
sec_pri9 = pd.merge(sec_pri9_date[['eid','combined']], sec_pri9_diag[['eid','combined']], on = 'eid', how = 'inner')

sec_pri10['datet'] = [item[1:] for item in sec_pri10['combined_x']]
sec_pri10['diagt'] = [item[1:] for item in sec_pri10['combined_y']]
def removenan(df): 
    datet2 = []
    diagt2 = []
    for i in range(df.shape[0]):
        temp1 = [x for x in df.iloc[i]['datet'] if type(x) != float]
        temp2 = [x for x in df.iloc[i]['diagt'] if type(x) != float]
        datet2.append(temp1)
        diagt2.append(temp2)
    df['datet2'] = datet2
    df['diagt2'] = diagt2


def zipper(df):
    result = []
    for i in range(df.shape[0]):
        temp = list(map(list, zip(df.iloc[i]['datet2'], df.iloc[i]['diagt2'])))
        result.append(temp)
    df['zipped'] = result
    print('end')
sec_pri9['datet'] = [item[1:] for item in sec_pri9['combined_x']]
sec_pri9['diagt'] = [item[1:] for item in sec_pri9['combined_y']]
removenan(sec_pri10)
zipper(sec_pri10)
removenan(sec_pri9)
zipper(sec_pri9)<jupyter_output>end<jupyter_text>Secondary Records secondary ICD<jupyter_code>sec_pri9['len'] = [len(x) for x in sec_pri9['zipped']]
sec_pri9_diag = sec_pri9[sec_pri9['len'] > 0]
sec_pri10['len'] = [len(x) for x in sec_pri10['zipped']]
sec_pri10_diag = sec_pri10[sec_pri10['len'] > 0]
def icdsearch(df, code):
    result = []
    if code == 10:
        for x in df['zipped']:
            temp = []
            if len(x) > 0:
                for y in x:
                    q = y[1][:3]
                    if q in ICD[10]['all']:
                        diag = icd10_codes[icd10_codes['ICD10'].str.contains(q)]['diagnosis'].values[0]
                        temp.append([datetime.strptime(y[0], '%Y-%m-%d').strftime('%d/%m/%Y'), diag, 'sec'])
            result.append(temp)
    elif code == 9:
        for x in df['zipped']:
            temp = []
            if len(x) > 0:
                for y in x:
                    q = str(y[1])[:3]
                    if q in ICD[9]['all']:
                        diag = icd9_codes[icd9_codes['ICD9'].str.contains(q)]['diagnosis'].values[0]
                        temp.append([datetime.strptime(y[0], '%Y-%m-%d').strftime('%d/%m/%Y'), diag, 'sec'])
            result.append(temp)
    df['coded'] = result
    print('end')
    
icdsearch(sec_pri10_diag,10)
icdsearch(sec_pri9_diag,9)
icdsearch(sec_pri10_diag,10)
sec_pri10_diag['codedlen'] = [len(x) for x in sec_pri10_diag['coded']]
sec_pri10_cvd = sec_pri10_diag[sec_pri10_diag['codedlen'] > 0]
sec_pri9_diag['codedlen'] = [len(x) for x in sec_pri9_diag['coded']]
sec_pri9_cvd = sec_pri9_diag[sec_pri9_diag['codedlen'] > 0]
sec_pri = sec_pri10_cvd[['eid','coded']]<jupyter_output><empty_output><jupyter_text>Self Reported<jupyter_code>sec_cli.shape
sec_cli.columns = ['eid', 'cardiac_0_a', 'cardiac_0_b', 'cardiac_0_c', 
                    'cardiac_0_d', 'cardiac_1_a', 'cardiac_1_b', 
                    'cardiac_1_c', 'cardiac_1_d', 'cardiac_2_a', 
                    'cardiac_2_b', 'cardiac_2_c', 'cardiac_2_d',
                    'MI_date', 'MI_source', 'STEMI_date', 
                    'STEMI_source', 'NSTEMI_date',
                    'NSTEMI_source', 'stroke_date', 'stroke_source',
                    'istroke_date', 'istroke_source', 
                    'intracerb_rrhage_d', 'intracerb_rrhage_s',
                    'subara_rrhage_d', 'subara_rrhage_s', 'gp_event_n'
       ]
cardiac_sec = sec_cli[(sec_cli.iloc[:, 1]==3.0) |
   (sec_cli.iloc[:, 2]==3.0) | 
   (sec_cli.iloc[:, 3]==3.0) |
   (sec_cli.iloc[:, 4]==3.0) |
   (sec_cli.iloc[:, 5]==3.0) |
   (sec_cli.iloc[:, 6]==3.0) |
   (sec_cli.iloc[:, 7]==3.0) |
   (sec_cli.iloc[:, 8]==3.0) |
   (sec_cli.iloc[:, 9]==3.0) |
   (sec_cli.iloc[:, 10]==3.0) |
   (sec_cli.iloc[:, 11]==3.0) |
   (sec_cli.iloc[:, 12]==3.0) |
   (sec_cli.iloc[:, 1]==1.0) |
   (sec_cli.iloc[:, 2]==1.0) | 
   (sec_cli.iloc[:, 3]==1.0) |
   (sec_cli.iloc[:, 4]==1.0) |
   (sec_cli.iloc[:, 5]==1.0) |
   (sec_cli.iloc[:, 6]==1.0) |
   (sec_cli.iloc[:, 7]==1.0) |
   (sec_cli.iloc[:, 8]==1.0) |
   (sec_cli.iloc[:, 9]==1.0) |
   (sec_cli.iloc[:, 10]==1.0) |
   (sec_cli.iloc[:, 11]==1.0) |
   (sec_cli.iloc[:, 12]==1.0)
  ]
#incidences
stroke0instance = []
stroke1instance = []
stroke2instance = []

# stroke is coded as 3
for i in range(0,len(cardiac_sec['eid'])): 
    if cardiac_sec.iloc[i]['cardiac_0_a']==3.0 or cardiac_sec.iloc[i]['cardiac_0_b']==3.0 or cardiac_sec.iloc[i]['cardiac_0_c']==3.0 or cardiac_sec.iloc[i]['cardiac_0_d']==3.0:
        stroke0instance.append(1)
    else: 
        stroke0instance.append(0)
    if cardiac_sec.iloc[i]['cardiac_1_a'] ==3.0 or cardiac_sec.iloc[i]['cardiac_1_b']==3.0 or cardiac_sec.iloc[i]['cardiac_1_c']==3.0 or cardiac_sec.iloc[i]['cardiac_1_d']==3.0:
        stroke1instance.append(1)
    else:
        stroke1instance.append(0)
    if cardiac_sec.iloc[i]['cardiac_2_a'] ==3.0 or cardiac_sec.iloc[i]['cardiac_2_b']==3.0 or cardiac_sec.iloc[i]['cardiac_2_c']==3.0 or cardiac_sec.iloc[i]['cardiac_2_d']==3.0:
        stroke2instance.append(1)
    else:
        stroke2instance.append(0)
HA0instance = []
HA1instance = []
HA2instance = []

for i in range(0,len(cardiac_sec['eid'])): 
    if cardiac_sec.iloc[i]['cardiac_0_a']==1.0 or cardiac_sec.iloc[i]['cardiac_0_b']==1.0 or cardiac_sec.iloc[i]['cardiac_0_c']==1.0 or cardiac_sec.iloc[i]['cardiac_0_d']==1.0:
        HA0instance.append(1)
    else: 
        HA0instance.append(0)
    if cardiac_sec.iloc[i]['cardiac_1_a'] ==1.0 or cardiac_sec.iloc[i]['cardiac_1_b']==1.0 or cardiac_sec.iloc[i]['cardiac_1_c']==1.0 or cardiac_sec.iloc[i]['cardiac_1_d']==1.0:
        HA1instance.append(1)
    else:
        HA1instance.append(0)
    if cardiac_sec.iloc[i]['cardiac_2_a'] ==1.0 or cardiac_sec.iloc[i]['cardiac_2_b']==1.0 or cardiac_sec.iloc[i]['cardiac_2_c']==1.0 or cardiac_sec.iloc[i]['cardiac_2_d']==1.0:
        HA2instance.append(1)
    else:
        HA2instance.append(0)

cardiac_sec['stroke0instance'] = stroke0instance
cardiac_sec['stroke1instance'] = stroke1instance
cardiac_sec['stroke2instance'] = stroke2instance 
cardiac_sec['HA0instance'] = HA0instance
cardiac_sec['HA1instance'] = HA1instance
cardiac_sec['HA2instance'] = HA2instance

cardiac_sec['stroke'] = (cardiac_sec['stroke0instance'] + cardiac_sec['stroke1instance']+ cardiac_sec['stroke2instance']) > 0
cardiac_sec['ha'] = (cardiac_sec['HA0instance'] + cardiac_sec['HA1instance']+ cardiac_sec['HA2instance']) > 0
# changing date formats
cardiac_sec['f_MI_date'] = [np.nan if type(x) == float else datetime.strptime(x, '%Y-%m-%d').strftime('%d/%m/%Y') for x in cardiac_sec['MI_date']]

cardiac_sec['f_stroke_date'] = [np.nan if type(x) == float else datetime.strptime(x, '%Y-%m-%d').strftime('%d/%m/%Y') for x in cardiac_sec['stroke_date']]
date_diag = []

for i in range(len(cardiac_sec['eid'])):
    recorded = []
    if cardiac_sec.iloc[i]['stroke'] == True: 
        if type(cardiac_sec.iloc[i]['f_stroke_date'])== float:
            recorded.append([np.nan])
        else:
            stroke_stat = [cardiac_sec.iloc[i]['f_stroke_date'], 'Stroke']
            recorded.append(stroke_stat)
            
    if cardiac_sec.iloc[i]['ha'] == True: 
        if type(cardiac_sec.iloc[i]['f_MI_date']) == float:
            recorded.append([np.nan])
        else:
            mi_stat = [cardiac_sec.iloc[i]['f_MI_date'], 'MI']
            recorded.append(mi_stat)
            
    date_diag.append(recorded)

cardiac_sec['date_diag'] = date_diag
cardiac_summ = cardiac_sec[['eid',
'stroke',  
'ha', 
'date_diag']]
cardiac_summ<jupyter_output><empty_output><jupyter_text>Primary care and Secondary care merge<jupyter_code>all_care = pd.merge(sec_pri, pri_records, how= 'outer', on = ['eid', 'eid'])
all_care
all_care['pri_date_diag'] = all_care['pri_date_diag'].apply(lambda x: x if isinstance(x, list) else [])
all_care['coded'] = all_care['coded'].apply(lambda x: x if isinstance(x, list) else [])
#. combine secondary and primary care dates
all_care['record_diag'] = all_care['coded'] + all_care['pri_date_diag']
records = pd.merge(all_care, pri_demo, how= 'left', on = ['eid', 'eid'])
records<jupyter_output><empty_output><jupyter_text>unique records and sorting records<jupyter_code># to make records non-duplicative
records['uniq_record_diag'] = [list(map(list,set(map(tuple,x)))) for x in records['record_diag'] if type(x) == list]
sort_record = []

for x in records['uniq_record_diag']:
    
    temp = []
    temp_pri = []
    temp_sec = []
    
    # handling nan
    for i in range(len(x)):
        if x[i] == [np.nan]:
            continue
        else: 
            #changing dates into datetime
            if type(x[i][0]) == str:
                yi0 = datetime.strptime(x[i][0], '%d/%m/%Y')
                yi1 = x[i][1]
                yi2 = x[i][2]
                temp.append([yi0, yi1, yi2])
                
    
    # sorting by dates
    temp.sort(key=lambda x: x[0])

    # append the sorted list
    sort_record.append(temp) 
records['sort_record'] = sort_record<jupyter_output><empty_output><jupyter_text>discrepancy coding<jupyter_code>records['yr_birth'] = [datetime.strptime(str(int(x)), '%Y') for x in records['34-0.0']]
# removing discrepancy from sort_record
sort_record2 = []
first_record_d =[]
first_record_diag = []
first_stroke_after_d = []
pri_record = []
sec_record = []
first_stroke_type = []

for i in range(records.shape[0]):
    temp = []
    temp_pri = []
    temp_sec = []
    
    recorded = records.iloc[i]['sort_record']
    dob = records.iloc[i]['yr_birth']
    
    if recorded == [np.nan]:
        temp.append([np.nan])
    else:
        
        for x in recorded:
            if x[0] <= dob:
                continue
            else:
                temp.append(x)
                if x[2] == 'pri':
                    temp_pri.append(x)
                elif x[2] == 'sec':
                    temp_sec.append(x)
                    
    sort_record2.append(temp)
    pri_record.append(temp_pri)
    sec_record.append(temp_sec)  
    
    # adding the first recorded dates and diag
    if len(temp) < 1:
        first_record_d.append(np.nan)
        first_record_diag.append(np.nan)
    elif len(temp) == 1:
        if temp[0] == [np.nan]:
            first_record_d.append(np.nan)
            first_record_diag.append(np.nan)
        else:
            first_record_d.append(temp[0][0])
            first_record_diag.append(temp[0][1])
    else:
        first_record_d.append(temp[0][0])
        first_record_diag.append(temp[0][1])
    
    # finding first incidence of stroke after the first incidence
    strokefound = False
    if len(temp) == 1: 
        first_stroke_after_d.append(np.nan)
        first_stroke_type.append(np.nan)
    else:
        for t in temp[1:]:
            if 'stroke' in t or 'tia' in t:
                strokefound = True
                first_stroke_after_d.append(t[0])
                first_stroke_type.append(t[1])
                print(i)
                break
                
        if strokefound == False:
            first_stroke_after_d.append(np.nan)
            first_stroke_type.append(np.nan)
records['sort_record2'] = sort_record2
records['first_record_d'] = first_record_d
records['first_record_diag'] = first_record_diag
records['first_stroke_after_d'] = first_stroke_after_d
records['first_stroke_type'] = first_stroke_type
records['pri'] = pri_record
records['sec'] = sec_record
records['discrepancy'] = records['first_record_d'] < records['yr_birth']
len(first_stroke_after_d)
records[records['discrepancy'] ==True]<jupyter_output><empty_output><jupyter_text>Records length<jupyter_code>records['noRecords'] = [0 if x == [[np.nan]] else len(x) for x in records['sort_record'] ]
records['noRecords2'] = [0 if x == [[np.nan]] else len(x) for x in records['sort_record2'] ]
records[records['first_stroke_after_d'].isnull() == False]<jupyter_output><empty_output><jupyter_text>interval calculation<jupyter_code>records['event_int'] = (records['first_stroke_after_d'] - records['first_record_d'])/np.timedelta64(1, 'M')<jupyter_output><empty_output><jupyter_text>report<jupyter_code>records[(records['noRecords'] > 1 ) & (records['event_int'].isnull() == False) & (records['discrepancy'] == False)].eid.count()
records[(records['noRecords'] > 1 ) & (records['event_int'] < 0.233) & (records['discrepancy'] == False)].eid.count()
records[(records['noRecords'] > 1 ) & (records['event_int'] >= 0.233) & (records['event_int'] < 1) & (records['discrepancy'] == False)].eid.count()
records[(records['noRecords'] > 1 ) & (records['event_int'] >= 1) & (records['event_int'] < 6) & (records['discrepancy'] == False)].eid.count()
records[(records['noRecords'] > 1 ) & (records['event_int'] >= 6) & (records['event_int'] < 12) & (records['discrepancy'] == False)].eid.count()
records[(records['noRecords'] > 1 ) & (records['event_int'] >= 12)  & (records['discrepancy'] == False)].eid.count()
records[(records['noRecords'] > 1 ) & (records['discrepancy'] == True)].eid.count()
records[(records['noRecords'] > 1 ) & (records['first_stroke_after_d'].isnull() == True)]
records[(records['noRecords'] > 1) & (records['first_stroke_after_d'].isnull() == True) & (records['discrepancy'] == False)]
records[records['first_stroke_type'].isnull() == False]<jupyter_output><empty_output><jupyter_text>Pickle<jupyter_code>final_records= records[(records['event_int'] >= 0) &(records['noRecords'] > 1) & (records['first_stroke_after_d'].isnull() == False) & (records['discrepancy'] == False)]
final_records
final_records = final_records[['eid', 'coded', 'uniq_record_diag', 'sort_record2',
                               'first_record_d', 'first_record_diag', 'first_stroke_after_d', 'first_stroke_type',
                               'pri', 'sec', 'yr_birth', 'noRecords', 'discrepancy', 'event_int']]
records.to_pickle('../primary_care/full_records.pkl')
final_records.to_pickle('../primary_care/records.pkl')
records
records.columns
records[records['noRecords'].isnull() == False]
records[(records['noRecords'] > 1 ) & (records['event_int'].isnull() == False) & (records['discrepancy'] == False)].first_record_diag.value_counts()
records[(records['noRecords'] > 1) & (records['discrepancy'] == False)]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup