I have a pandas dataframe below:
df
name value1 value2 otherstuff1 otherstuff2
0 Jack 1 1 1.19 2.39
1 Jack 1 2 1.19 2.39
2 Luke 0 1 1.08 1.08
3 Mark 0 1 3.45 3.45
4 Luke 1 0 1.08 1.08
Same name
will have the same value for otherstuff1
and otherstuff2
.
I'm trying to group by column name
and sum both columns value1
and value2
. (Not sum value1
with value2
!!! But sum them individually in each column.)
Expecting to get result below:
newdf
name value1 value2 otherstuff1 otherstuff2
0 Jack 2 3 1.19 2.39
1 Luke 1 1 1.08 1.08
2 Mark 0 1 3.45 3.45
I've tried
newdf = df.groupby(['name'], as_index=False).sum()
which groups by name
and sums up both value1
and value2
columns correctly, but ends up dropping columns otherstuff1
and otherstuff2
.