Sel
Sel

Reputation: 21

How to merge multiple dataframe rows into one by key?

I have a pandas dataframe like this:

key     columnA
1        1199
1        8674
2        8674
2        0183
2        3957
3        0183
3        3647

Expected result:

key      columnA
1       11998674
2       867401833957
3       01833647

Is there sth. that merges the rows by key while putting the different values in columnA together?

Upvotes: 0

Views: 49

Answers (1)

warped
warped

Reputation: 9481

df['columnA'] = df['columnA'].astype(str) 

method 1:

df.groupby('key').agg({'columnA': sum})

method 2:

df.groupby('key').agg({'columnA': "".join})

optionally, convert the column back to int.

if you want to add separators:

# assuming separator is ":"

df.groupby('key').agg({'columnA': ":".join})

Upvotes: 1

Related Questions