df=pd.DataFrame({'A':['Bac 3','Bac 3','Bac 3'],
'B':['Bac 6','Bac 6','Bac 8'],
'A_len':[39577,39577,39577],
'B_len':[40449,40449,38091],
'A_prot_len':[500,550,800],
'B_prot_len':[450,300,600]})
A B A_len B_len A_prot_len B_prot_len
0 Bac 3 Bac 6 39577 40449 500 450
1 Bac 3 Bac 6 39577 40449 550 300
2 Bac 3 Bac 8 39577 38091 800 600
The dataframe has been sorted by A and B whereby their order of occurrence is linked. I've made a function to calculate distances between unique pairs of A and B but would like to present the result in a matrix.
def dist_species(df):
frames = []
unique_B = list(set(df['B']))
for species in unique_B:
df = df.copy()
A_B_pairs = df[df['B'] == species]
x = {'A':'first','B':'first', 'A_len': 'first', 'B_len': 'first', 'A_prot_len':'sum', 'B_prot_len': 'sum'}
A_B = A_B_pairs.groupby(['A','B']).agg(x).reset_index(drop=True)
A_B['Dist'] = 1-(A_B['A_prot_len'] + A_B['B_prot_len']) / (A_B['A_len'] + A_B['B_len'])
frames.append(A_B)
return pd.concat(frames)
dist_species(df)
A B A_len B_len A_prot_len B_prot_len Dist
0 Bac 3 Bac 6 39577 40449 1050 750 0.977507
0 Bac 3 Bac 8 39577 38091 800 600 0.981975
How can I structure my output to this:
| | Bac 3 | Bac 6 | Bac 8 |
| --------| --------|----------|----------|
| Bac 3 | 1 | 0.977507 | 0.981975 |
| Bac 6 | 0.977507| 1 | 0 |
| Bac 8 | 0.981975| 0 | 1 |
Really appreciate any guidance, thank you!