0

Im trying to:

count_values to count the occurrences of the same value under invoice_line_normal_price and groupby shop_name

This here is my df:

Here is my dataframe

I'm trying to end up with:

Desired Result

So far i know I can use:

df.value_counts('invoice_line_normal_price').to_frame('counts')

to get the counts without the grouping, but how would I add the grouping here?

Any help would be greatly appreciated, thanks.

  • So need `df.groupby(['shop_name'])['invoice_line_normal_price'].value_counts()` ? – jezrael Apr 20 '21 at 13:31
  • For counts check secon danswer in changed dupe. – jezrael Apr 20 '21 at 13:32
  • Thats good for me @jezrael. Just need to know how to have the shop_name showing on every line.. tried: nr.groupby((['shop_name']),as_index=False)['invoice_line_normal_price'].value_counts() but I get AttributeError: 'DataFrameGroupBy' object has no attribute 'value_counts' – BenniBenassi Apr 20 '21 at 13:53
  • I think problem is duplicated column `invoice_line_normal_price`, check it by `print(df['invoice_line_normal_price'])` if return 2 or more columns. – jezrael Apr 20 '21 at 15:38
  • When I try this I get: Traceback (most recent call last): Python/CouponRedemptions/start.py", line 22, in print(v['invoice_line_normal_price']) File "/home/ben/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 2902, in __getitem__ indexer = self.columns.get_loc(key) File "~/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2893, in get_loc raise KeyError(key) from err KeyError: 'invoice_line_normal_price' – BenniBenassi Apr 22 '21 at 10:14

0 Answers0