1

I want to make a list of the sums of all the goods in the order. If I write like that:

df = data.groupby(['order_id'])
prices = []
prices.append(
                sum(
                    list(
                        df.get_group(3)['item_price']
                        )
                    )
                 )

then everything is fine, I have the total price of a check for the 3 order:

[12.67]

But if I do like that:

df = data.groupby(['order_id'])
prices = []

for i in range(len(df['order_id'])):
    prices.append(
                sum(
                    list(
                        df.get_group(i)['item_price']
                        )
                    )
                 )

then I have the error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-71-abadd8c807d6> in <module>
      6                 sum(
      7                     list(
----> 8                         df.get_group(i)['item_price']
      9                         )
     10                     )

~/anaconda3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py in get_group(self, name, obj)
    646         inds = self._get_index(name)
    647         if not len(inds):
--> 648             raise KeyError(name)
    649 
    650         return obj._take(inds, axis=self.axis)

KeyError: 0

How to fix this?

1
  • 1
    Can you post an example input dataframe and your expected output? Commented Jun 26, 2020 at 16:09

3 Answers 3

2

Does this do it?

df.groupby('order_id')['item_price'].sum().tolist()
Sign up to request clarification or add additional context in comments.

Comments

0

You can try to iterate with iterrows method and use the index variable

df = data.groupby(['order_id'])
prices = []


for index, row in df.iterrows():
    prices.append(
                sum(
                    list(
                        row['item_price']
                        )
                    )
                 )

Comments

0

Without knowing the data is not that simple, but the main idea is the following: get_group uses the name of the group, which normally is heredity by the group column, in this case order_id so if you don't have an order_id==0 the KeyError will be raised.

If you want to proceed with this approach, do a loop without the range/len function

df = data.groupby('order_id')
prices = []

for label, group in df:
    prices.append(
                sum(
                    list(
                        group['item_price']
                        )
                    )
                 )

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.