Max AweTery
Max AweTery

Reputation: 103

Convert DataFrame into list by groups

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?

Upvotes: 1

Views: 91

Answers (3)

jcaliz
jcaliz

Reputation: 4021

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']
                        )
                    )
                 )

Upvotes: 0

L. Quastana
L. Quastana

Reputation: 1336

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']
                        )
                    )
                 )

Upvotes: 0

Adam Zeldin
Adam Zeldin

Reputation: 908

Does this do it?

df.groupby('order_id')['item_price'].sum().tolist()

Upvotes: 2

Related Questions