python - pandas - combine strings of row -


i have dataframe this:

    id        text     1         dm,htn,enlarged prostate     2         hypertensive , on regular treatment     2         lbp     3         dm,htn,enlarged prostate 

i want combine text of same id this:

   id        text    1         dm,htn,enlarged prostate    2         hypertensive , on regular treatment lbp    3         dm,htn,enlarged prostate 

the texts of id 2 combined. how can acchieve this? appreciated.

you can use groupby , apply function join. last reset_index:

grouped_df = df.groupby("id")['text'].apply(' '.join).reset_index() print (grouped_df)    id                                       text 0   1                   dm,htn,enlarged prostate 1   2  hypertensive , on regular treatment lbp 2   3                   dm,htn,enlarged prostate 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -