hexi
hexi

Reputation: 39

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

I am fairly new to python and pandas so sorry for the beginners question but, I have not yet found a solution to do this simple task.

Dataframe:

Org        Data1       Data2
1          1234        Win
1          2345        Win
2                      Loss
3          3456        Win
3          4567        Win

I've been trying to use this groupby and apply lambda

df.groupby(["Org"])["Data1", "Data2"].apply(lambda x: ';;'.join(x.astype(str)))

Which does not function properly since the outcome is only

Org 
1    Data1;;Data2
2    Data1;;Data2
3    Data1;;Data2

This is what I would like to achieve:

Org        Data1            Data2
1          1234 ;; 2345     Win ;; Win
2          NaN              Loss
3          3456 ;; 4567     Win ;; Win

The Org represents the defined index which I want to use to group it by. The same Org answers "1 2 3" to Data1 and Data2 should go to the same cell in excel, which I then want to print out as a completely new excel file.

Can anyone help me with this fairly simple (but somehow difficult for me) issue?

Upvotes: 1

Views: 2570

Answers (1)

jezrael
jezrael

Reputation: 862641

You are really close, only 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

Upvotes: 4

Related Questions