DDRRpy DDRRpy - 1 month ago 6
Python Question

Assess whether duplicate samples have different data in their fields and whether to copy the data over?

I want to assess whether a sample and its duplicate (ending with a _2) have data entered in their Age, Family History and Diagnosis field. If one sample has entries and its duplicate doesn't (all "-" entries) then I want to copy the entries from the sample to the duplicates fields. The assessment should work the other way around to: if the duplicate has entries and the sample doesn't then copy them over to the sample fields.

Basically, I want the input_df to look like the desired_df (shown below).

input_df = pd.DataFrame(columns=['Sample', 'Date','Age', 'Family History', 'Diagnosis'],
data=[
['HG_12_34', '12/3/12', '23', 'Y', 'Jerusalem Syndrome'],
['LG_3_45', '3/4/12', '45', 'N', 'Paris Syndrome'],
['HG_12_34_2', '4/5/13', '-', '-', '-'],
['KD_89_9', '8/9/12', '-', '-', '-'],
['KD_98_9_2', '6/1/13', '54', 'Y', 'Chronic Hiccups'],
['LG_3_45_2', '4/4/10', '59', 'N', 'Dangerous Sneezing Syndrome']
])

desired_df = pd.DataFrame(columns=['Sample', 'Date','Age', 'Family History', 'Diagnosis'],
data=[
['HG_12_34', '12/3/12', '23', 'Y', 'Jerusalem Syndrome'],
['LG_3_45', '3/4/12', '45', 'N', 'Paris Syndrome'],
['HG_12_34_2', '4/5/13', '23', 'Y', 'Jerusalem Syndrome'],
['KD_89_9', '8/9/12', '54', 'Y', 'Chronic Hiccups'],
['KD_98_9_2', '6/1/13', '54', 'Y', 'Chronic Hiccups'],
['LG_3_45_2', '4/4/10', '59', 'N', 'Dangerous Sneezing Syndrome']
])


Below details my really inefficient and incomplete attempt at this:

def testing(duplicate, df):
''' Checking difference in phenotype data between duplicates
and return the sample name if
'''
# only assess the duplicate
if duplicate['Sample'][:-2] in list(df['Sample'].unique()):

# get sam row
sam = df[df['Sample'] == duplicate['Sample'][:-2]]

# store the Age, Family History and Diagnosis in a list for each sample
sam_pheno = sam.iloc[0][2:4].fillna("-").tolist()
duplicate_pheno = duplicate[2:4].fillna("-").tolist()

# if the duplicate sample has nothing in these fields then return the
# orginal sample name
if len(set(duplicate_pheno)) == 1 and list(set(duplicate_pheno))[0] == "-" \
and len(set(sam_pheno)) > 1:
return duplicate['Sample'][:-2]






# this creates a column called Pheno which has the name of the sample which contains the phenotype data that they should share. This is intended so that I can somehow copy over the phenotype data from the sample name in the Pheno field. However, I have no idea how to do this.
input_df['Pheno'] = input_df.apply(lambda x: testing(x, input_df), axis =1)

Answer

You can use:

#replace all - values to NaN
input_df = input_df.replace('-',np.nan)
#all values end with _2 and longer as 7
mask = (input_df.Sample.str.endswith('_2')) & (input_df.Sample.str.len() > 7)
#create new columnn same with column Sample + remove last 2 chars (_2)
input_df.ix[mask, 'same'] = input_df.ix[mask, 'Sample'].str[:-2]
#replace NaN in same by Sample column
input_df.same = input_df.same.combine_first(input_df.Sample)
#sort values
input_df = input_df.sort_values(['same','Family History'], ascending=False)
#replace NaN by forward filling
input_df[['Age','Family History','Diagnosis']] = 
input_df[['Age','Family History','Diagnosis']].ffill()
#get original index by sorting
input_df.sort_index(inplace=True)
#remove column same
input_df.drop('same', axis=1, inplace=True)

print (input_df)     
       Sample     Date Age Family History                    Diagnosis
0    HG_12_34  12/3/12  23              Y           Jerusalem Syndrome
1     LG_3_45   3/4/12  45              N               Paris Syndrome
2  HG_12_34_2   4/5/13  23              Y           Jerusalem Syndrome
3     KD_89_9   8/9/12  54              Y              Chronic Hiccups
4   KD_98_9_2   6/1/13  54              Y              Chronic Hiccups
5   LG_3_45_2   4/4/10  59              N  Dangerous Sneezing Syndrome

print (desired_df)                   
       Sample     Date Age Family History                    Diagnosis
0    HG_12_34  12/3/12  23              Y           Jerusalem Syndrome
1     LG_3_45   3/4/12  45              N               Paris Syndrome
2  HG_12_34_2   4/5/13  23              Y           Jerusalem Syndrome
3     KD_89_9   8/9/12  54              Y              Chronic Hiccups
4   KD_98_9_2   6/1/13  54              Y              Chronic Hiccups
5   LG_3_45_2   4/4/10  59              N  Dangerous Sneezing Syndrome