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?