1

I have a DataFrame with the following structure:

+----------+--------+-------+---------------+
|     cash |  game  |item_id|username       |
+-------------------+-------+---------------+
| 0  10.42 |  test1 |   131 |    my_name_1  |
| 1   4.45 |  test2 |   133 |   my_name_2   |
| 2  123.2 | test1  |    23 |    my_name_1  |
+------------------+--------+---------------+

I need to write a method that takes this DataFrame as input and returns the sum of money each user has spent per game. The output should look like this:

username    game 
my_name_1   test1 133.62
my_name_2   test2 4.45

Any input on how I can do this is appreciated.

1 Answer 1

1

Use groupby with aggregating sum and parameter as_index=False for return DataFrame:

print (df.groupby(['username','game'], as_index=False)['cash'].sum())
    username   game    cash
0  my_name_1  test1  133.62
1  my_name_2  test2    4.45

Or add reset_index:

print (df.groupby(['username','game'])['cash'].sum().reset_index())
    username   game    cash
0  my_name_1  test1  133.62
1  my_name_2  test2    4.45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.