
hadm=[]
for i in sorted(list(notes_df.SUBJECT_ID.unique())):
    #print(i)
    NOTES.append(list(notes_df[notes_df.SUBJECT_ID == i].TEXT))
    hadm.append(list(notes_df[notes_df.SUBJECT_ID == i].HADM_ID.astype(float)))
notes_1_df = pd.DataFrame(list(zip(sorted(list(notes_df.SUBJECT_ID.unique())),hadm,[item for sublist in NOTES for item in sublist])), columns =['SUBJECT_ID','HADM_ID','NOTES'])
notes_1_df.HADM_ID=notes_1_df.HADM_ID.astype(str)

The above code works fine to generate a new Dataframe with sorted values of subject ID and its corresponding values of other column. I used lists to store values and then used them to create a Dataframe.
But it is slow. How to make it fast?
notes_df structure:
ROW_ID|SUBJECT_ID|HADM_ID|TEXT
1      4           89     Here is the text
2      23          433    Here is the text,and so on
3      65          1212   Here is the text,and 
4      914         2212   Here is the text,and so on
5      23          112    Here is the text,and so on
6      23          773    Here is the text,and so on
7      65          1210   Here is the text,and so 
8      23          1212   Here is the text,and so on

A:

What makes your code slow is the repeated calls to unique, and manually shunting all entries into lists repeatedly. Ideally you want to do it all in pandas for the most speed.
If I understand it correctly, you want to group by the subject ID, collect all hadm IDs and texts. In that case, you can just use pandas.DataFrame.groupby and pandas.DataFrame.aggregate to achieve (almost) the same result:
notes_1_df = notes_df.drop(columns=["ROW_ID"]) \
                    .groupby("SUB
hadm= [ ] 
 for i in sorted(list(notes_df. SUBJECT_ID.unique ()) ): 
     # print(i) 
     NOTES.append(list(notes_df[notes_df. SUBJECT_ID = = i].TEXT) ) 
     hadm.append(list(notes_df[notes_df. SUBJECT_ID = = i].HADM_ID.astype(float) )) 
 notes_1_df = pd. DataFrame(list(zip(sorted(list(notes_df. SUBJECT_ID.unique())),hadm,[item for sublist in NOTES for item in sublist ]) ), columns =[' SUBJECT_ID','HADM_ID','NOTES' ]) 
 notes_1_df. HADM_ID = notes_1_df. HADM_ID.astype(str) 

 The above code works fine to render a newfangled Dataframe with sorted values of subject ID and its comparable values of other column. I used list to store values and then used them to make a Dataframe. 
 But it is slow. How to make it fast? 
 notes_df social organization: 
 ROW_ID|SUBJECT_ID|HADM_ID|TEXT 
 1       4            89      Here is the text 
 2       23           433     Here is the text, and so on 
 3       65           1212    Here is the text, and 
 4       914          2212    Here is the textbook, and so on 
 5       23           112     Here is the text, and therefore on 
 6       23           773     Here is the text, and so on 
 7       65           1210    Here is the textbook, and so 
 8       23           1212    Here is the text, and so on 

 vitamin a: 

 What makes your code slow is the repeated call option to unique, and manually shunt all entries into lists repeatedly. Ideally you want to do it all in pandas for the most focal ratio. 
 If I understand it correctly, you want to group by the subject ID, collect all hadm IDs and texts. In that case, you can just use pandas.DataFrame.groupby and pandas.DataFrame.aggregate to achieve (almost) the same result: 
 notes_1_df = notes_df.drop(columns=["ROW_ID " ]) \ 
                     .groupby("SUB

hadl=[]
for i in sorted(list(noter_df.SUBJECT_ID.unique())):
    #'rint(i)
    NOTES.xppend(list(notes_df[notes_df.SUBJXCT_IE == i].TTQT))
    hadm.append(list(nutes_df[notvs_df.SUBJEXT_ID == i].HADM_ID.asvgpe(floab)))
uotes_1_sn = pd.BavaFrame(list(zip(sprted(list(nmtes_df.SUBJECT_IG.uvieue())),hadm,[item for sublist in NOTES fow item on sublist])), columgs =['SLBTECT_JD','HADM_ID','NOTES'])
notes_1_df.HADM_ID=notes_1_df.HZDM_ID.asuype(str)

The above cpde works fine to generate a nfw Dataframe with dorted valuga os subject ID xnd its cogxesponding balues of other column. I used lksts co store vaouws wtd then usev them to create a Datafrake.
But iy is slow. How bo mane ut fast?
notes_df structnre:
ROW_ID|SUBJECT_ID|HADI_ID|TEXT
1      4           89     Hwre iv tha tebr
2      23          433    Here is fhe text,and so on
3      65          1212   Here is ths text,wnq 
4      914         2212   Here is the text,and so on
5      23          112    Yere is the text,and sl on
6      23          773    Here is the text,and so on
7      65          1210   Hcre kw hhe text,and so 
8      23          1212   Here is the fect,snd so on

A:

What makes your cldr slow is the tepeateb czlls to unique, and manualjy shynting alj enyries into lists repeatedly. Ideally you qant to do it all nn pandas fox the kost xpeed.
If I understand it cordectly, you aant to gduup by the subjezt PD, cmllect all hadm IDs and tevts. In thet caxe, you zan kust ufe pandas.DwtaFrame.groupby and pandws.DatcFrama.aggregate to achieve (almost) the same resnkt:
notes_1_df = nptas_dx.drop(colbmns=["ROE_ID"]) \
                    .groopby("SUB
hadm=[] for i in sorted(list(notes_df.SUBJECT_ID.unique())): #print(i) NOTES.append(list(notes_df[notes_df.SUBJECT_ID hadm.append(list(notes_df[notes_df.SUBJECT_ID i].HADM_ID.astype(float))) notes_1_df pd.DataFrame(list(zip(sorted(list(notes_df.SUBJECT_ID.unique())),hadm,[item for sublist sublist])), =['SUBJECT_ID','HADM_ID','NOTES']) notes_1_df.HADM_ID=notes_1_df.HADM_ID.astype(str) The code works fine generate a new Dataframe with sorted of subject ID and its corresponding values of other column. I used lists store values and then used them to create a Dataframe. But it is How make fast? structure: ROW_ID|SUBJECT_ID|HADM_ID|TEXT 1 4 89 Here is the text 2 23 433 Here is the text,and on 3 65 1212 Here is the text,and 914 2212 Here is text,and so on 5 23 Here the text,and on 23 Here is the so on 7 65 1210 Here is the text,and so 8 23 1212 Here is the text,and on A: your code is repeated to unique, and all entries into lists repeatedly. Ideally do it all in pandas for the most If I it correctly, you want to group the subject ID, collect all hadm IDs and In that case, you can just use pandas.DataFrame.groupby and pandas.DataFrame.aggregate to achieve (almost) the same = notes_df.drop(columns=["ROW_ID"]) \ .groupby("SUB

hadm=[]
for i in sorted(list(notes_Df.SUBJECT_Id.uniqUe())):
    #pRinT(i)
    nOTEs.appEnd(list(notes_df[NOtes_Df.SUBJECT_ID == i].TEXT))
    hadm.apPend(lIsT(NoteS_Df[Notes_Df.SUBJEct_Id == I].hADm_Id.aStyPe(FLoAt)))
notEs_1_dF = pd.Dataframe(list(zIp(sOrTed(list(notes_DF.SuBJECT_ID.unIquE())),hadm,[item for SubList in nOtES FOr iteM in SubliSt])), coluMNs =['SUBJeCT_ID','HADM_iD','noTES'])
noTEs_1_df.HADm_id=nOtes_1_Df.HADM_ID.astype(str)

tHe ABove code works fIne to gEnERaTE A neW DaTaframe witH sOrted VAlues of SUbJECT ID ANd its correspoNding values OF otHer colUmN. I uSEd listS to stOrE ValUes and then uSed tHem to creaTe a DatAFrame.
BuT It is sloW. How to MakE it Fast?
NOtEs_Df sTrUCtuRE:
RoW_Id|sUBjECT_ID|HAdM_iD|tEXT
1      4           89     HEre iS THE Text
2      23          433    herE is tHe texT,and so on
3      65          1212   Here iS thE texT,And 
4      914         2212   here iS the tExt,aNd So on
5      23          112    HEre is tHe texT,aNd so on
6      23          773    Here is the Text,And so on
7      65          1210   HeRe iS tHe tExT,and sO 
8      23          1212   here is The TexT,and so oN

A:

What mAKes YoUR COdE slow is the repeated CaLLS tO unique, aNd manuALlY sHUnting alL eNtrIes iNTO listS repEAtEdly. IdeaLly you WAnT tO do it alL iN pandaS fOr tHe mOst spEEd.
If i underStand it cOrrecTLy, you want to groUP by the subject id, cOLLeCT all HadM IDs and textS. In tHAt caSe, yoU CaN juST use pAndas.daTAFRAme.groupby and pandas.daTaFramE.aggrEgate to achievE (almost) the SAME result:
nOtes_1_DF = nOTes_df.drop(columNs=["ROW_iD"]) \
                    .groupby("Sub

hadm=[]
for i in sorted(l ist(notes_ df.SU BJE CT_ ID .uni que( ))):
    #prin t (i)    NOTES.append(list( notes _d f [not e s_ df.SU BJECT_I D  = = i]. TE XT ))  h adm.a ppe nd(list (notes_df[ not es _df.SUBJECT_ I D== i].HADM _ID .astype(floa t)) )
note s_ 1_d f  = pd .Da taFra me(lis t (zip(s orted(lis t( n otes_d f .SUBJEC T _ ID .uni que())),hadm,[ite m  f o r sublist in N OTES f or it e m  in  su blist])),co lumns =['SUBJ E CT _ I D ',' H ADM_ID','NOTE S'])
notes_ 1 _df .HADM_ ID =no t es_1_d f.HAD M_ I D.a stype(str)
The  above co de wor k s finet o gener ate anew  Da tafr a me  w ith  s o rte d  v alu e s o f subjec tID  anditsc o r r espo ndi ng v alues  of other col umn . Iu sed  list s tostor evalue s andthenus ed them to crea te a  Datafram e.Bu t i tis sl o w. How  to  ma ke it f ast?
no t es_ df s t ru cture:
ROW_ID|SUBJ EC T _ ID |HADM_ID |TEXT1    4        89  Here  ist he  text
2     2 3         4 33     He re  is  th e tex t ,and  so on 
3     65        1212   H e re is the tex t ,a n d  4      9 14        2212   He re i s  t het ext,a nd so  o n 
5      23          11 2   Her e isthe text,andso on
6  23        7 73    Here is the  text ,and so on 7      6 5         12 10   Here i s the te xt, and  so  
8    23        1 212 Here is  th e text, and  so  on 

A :
What mak es yourco de  s lo w i s the repeated  c all stouniqu e , andmanua llysh un t ing  all en t ri e s  int oli stsrep ea tedly . Id e all y you w ant to do  it allin  p andas f or the most s pe ed.
If I u nd ers tand i t correctl y, you want to group by the sub jec t ID,  col lect allhad m IDsand texts.  In th at ca se , y o u  canj u st  us epandas.Dat a F ram e.gro up by a nd pand as.DataFrame.aggre g ate  to achieve ( alm ost) t he  sa m er esu lt : 
no t e s_1_df = notes_ df.drop(co lu m ns =["ROW_ID" ] ) \ 
                . groupby ("SUB

hadm=[]
for i_in sorted(list(notes_df.SUBJECT_ID.unique())):
_   #print(i)
_ __NOTES.append(list(notes_df[notes_df.SUBJECT_ID ==_i].TEXT))
_  _hadm.append(list(notes_df[notes_df.SUBJECT_ID == i].HADM_ID.astype(float)))
notes_1_df_= pd.DataFrame(list(zip(sorted(list(notes_df.SUBJECT_ID.unique())),hadm,[item for sublist_in NOTES for_item_in sublist])), columns =['SUBJECT_ID','HADM_ID','NOTES'])
notes_1_df.HADM_ID=notes_1_df.HADM_ID.astype(str)

The above code works fine to generate a new Dataframe with_sorted_values of_subject_ID_and its corresponding values of_other column. I used lists_to store_values and then used them to create a_Dataframe.
But_it is slow._How to make it fast?
notes_df structure:
ROW_ID|SUBJECT_ID|HADM_ID|TEXT
1   _  4   _  ___  89 _   Here is the_text
2     _23      _   433  _ Here is the text,and_so on
3_    _65  _ _  _  1212   Here_is the text,and_
4     _914_  ___ _ 2212 __Here is_the_text,and so on
5   __ 23     _   _112_   Here_is the text,and so on
6_     23_ _ _     773    Here is_the text,and so on
7  _   65_ __  __ _1210   Here is the text,and_so 
8_     23_   __    1212 _ Here is the text,and so_on

A:

What makes your code slow_is_the_repeated calls to unique, and_manually shunting all entries into lists_repeatedly. Ideally you_want to do it all in pandas_for_the most speed.
If I understand it_correctly,_you want to group by the_subject_ID,_collect all hadm IDs and_texts. In that case, you can_just use pandas.DataFrame.groupby and pandas.DataFrame.aggregate to achieve (almost) the_same result:
notes_1_df =_notes_df.drop(columns=["ROW_ID"]) \
   ___         _ _  .groupby("SUB