pariskey
pariskey

Reputation: 53

Add values from rows based on conditions from two columns?

I have a data-frame that looks like this:

time name count variable4
0500 unit1 4 nan
0500 unit1 1 1
0500 unit2 2 nan
0500 unit3 4 nan
0500 unit3 1 2
0600 unit1 8 nan

What I want is this:

time name count variable4
0500 unit1 5 1
0500 unit2 2 nan
0500 unit3 5 2
0600 unit1 8 nan

Basically, I want to combine the rows and add the values in the count and variable4 column if the values in both time and name are equal. I've tried groupby with sum() as df.groupby(['time','name'])(['count', 'variable4']).sum() but the error says DataFrameGroupBy object is not callable. I don't think I can pass two columns into sum like that, but I am unsure how else to do it. TIA.

Upvotes: 0

Views: 64

Answers (2)

eroot163pi
eroot163pi

Reputation: 1815

To the object df.groupby(['time','name']) you are calling by passing ['count', 'variable4'] as argument. So you can do this only on some special classes called Callable classes refer. Thats where you are getting error.

Replace () enclosing ['count', 'variable4'] after groupby to []

df.groupby(['time','name'])[['count', 'variable4']].sum()

Upvotes: 1

mozway
mozway

Reputation: 260975

Your parentheses are invalid, just do:

df.groupby(['time','name']).sum()

Or:

df.groupby(['time','name'])[['count', 'variable4']].sum()

Upvotes: 1

Related Questions