Skip to content
Advertisement

Pandas data frame how to make a scatter plot for clustering a list of values into a set of groups

I have this pandas df with 2 columns

     target                        drugs

0   ACE2 gene                [angiotensin II,rosiglitazone,irbesartan,valsar]
1   Elastases                [heparin,prednisolone,montelukast,formoterol]
2   MAPK14 protein           [oxaprozin,nilotinib,imatinib,tocilizumab]
3   TMPRSS2 gene             [enzalutamide,camostat]
4   Toll-like receptors      [rituximab,atorvastatin,artesunate,tazarotene]
5   Ubiquitin                [sunitinib,lapatinib,atorvastatin,edaravone]
6   ezrin                    [erlotinib,crizotinib,sorafenib,everolimus]

and I want to create a plot that clusters the drugs into their target, so there will be 7 clusters (7 targets) , I am not sure how to do it..

This is the df:

import pandas as pd
    
data = {'target': ['ACE2 gene', 'Elastases', 'MAPK14 protein, human', 'TMPRSS2 gene', 'Toll-like receptors', 'Ubiquitin' , 'ezrin'],'drugs': [['angiotensin II','rosiglitazone','irbesartan'], ['heparin','prednisolone','montelukast','formoterol'] , ['oxaprozin','nilotinib','imatinib','tocilizumab'] , ['enzalutamide','camostat'] , ['rituximab','atorvastatin','artesunate','tazarotene'] , ['sunitinib','lapatinib','atorvastatin','edaravone'], ['erlotinib','crizotinib','sorafenib','everolimus']]
}
    
df = pd.DataFrame(data)

Advertisement

Answer

You can plot scatterplot with seaborn like below: (Because you say in the comments of other answer, You have problem with answer, I send another answer with another approach.)

import matplotlib.pyplot as plt
import pandas as pd
from itertools import chain
import seaborn as sns
    
df = pd.DataFrame(data = {'target': ['ACE2 gene', 'Elastases', 'MAPK14 protein, human', 'TMPRSS2 gene', 'Toll-like receptors', 'Ubiquitin' , 'ezrin'],
                          'drugs': [['angiotensin II','rosiglitazone','irbesartan'], ['heparin','prednisolone','montelukast','formoterol'] , 
                                    ['oxaprozin','nilotinib','imatinib','tocilizumab'] , ['enzalutamide','camostat'] , 
                                    ['rituximab','atorvastatin','artesunate','tazarotene'] , ['sunitinib','lapatinib','atorvastatin','edaravone'], 
                                    ['erlotinib','crizotinib','sorafenib','everolimus']]})

df['times'] = df['drugs'].apply(lambda x : len(x))
df = df.loc[df.index.repeat(df['times'])].reset_index(drop=True)
df['drug'] = df.groupby('target')['drugs'].transform(lambda x: list(y[idx] for idx, y in enumerate(x)))
df = df.drop(['drugs','times'], axis=1)
df['unq_id'] = df.index+1


fig, axe = plt.subplots(figsize=(20,10))
axe.axis('off')
sns.scatterplot(data=df, x="unq_id", y="target", hue="target", ax= axe, s=1000)
for _, point in df.iterrows():
  axe.text(point['unq_id']-0.2, point['target'], point['drug'], rotation=45, size=18)

plt.setp(axe.get_legend().get_texts(), fontsize='22') # for legend text
plt.setp(axe.get_legend().get_title(), fontsize='32') # for legend title
plt.show()

Output:

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement