0

This is the data I have been given:

input dataframe

I have been wanting to take the sum of the needed supplies with respect to their location but the groupby function does not seem to work. For example: df..groupby(['location']).sum()

However this gives the following, which is not the desired output.

output dataframe

6
  • 1
    What is your expected result because groupby.sum is working as expected based on your screenshot? Commented Apr 13, 2022 at 20:03
  • The expected result should be along the lines of: location 6 needs 1 + 3 = 4 supplies and location 22 needs 3+8 = 11 supplies. Commented Apr 13, 2022 at 20:07
  • You are only showing the first 10 rows in your screenshot but your frame is larger. There are other locations that are being grouped and summed. Commented Apr 13, 2022 at 20:08
  • Indeed this is a subset of a larger problem, however i could not find a solution for the whole dataframe so i figured that if there is a solution for the first 10 it should work for (in my case) 200 rows total. But i still dont get why on this subset the sum function is incorrect Commented Apr 13, 2022 at 20:13
  • 1
    The issue is that you have strings. Thus '1'+'3' = '13' ; '3'+'8' = '38'. Convert to integers, then your logic will work. Commented Apr 13, 2022 at 20:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.