Reputation: 21
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
Reputation: 9481
df['columnA'] = df['columnA'].astype(str)
df.groupby('key').agg({'columnA': sum})
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