python - Merging several rows with the same index on a single data frame? -


i new python , pandas sorry beginners question but, have not yet found solution simple task.

dataframe:

org        data1       data2 1          1234        win 1          2345        win 2                      loss 3          3456        win 3          4567        win 

i've been trying use groupby , apply lambda

df.groupby(["org"])["data1", "data2"].apply(lambda x: ';;'.join(x.astype(str))) 

which not function since outcome

org  1    data1;;data2 2    data1;;data2 3    data1;;data2 

this achieve:

org        data1            data2 1          1234 ;; 2345     win ;; win 2          nan              loss 3          3456 ;; 4567     win ;; win 

the org represents defined index want use group by. same org answers "1 2 3" data1 , data2 should go same cell in excel, want print out new excel file.

can me simple (but somehow difficult me) issue?

you close, need agg instead apply:

df = df.groupby(["org"])["data1", "data2"].agg(lambda x: ';;'.join(x.astype(str))) print (df)               data1     data2 org                           1    1234.0;;2345.0  win;;win 2               nan      loss 3    3456.0;;4567.0  win;;win 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -