1

I am trying to add a column to my dataframe that tells me the number of a product per group. My dataframe looks like this:

ID    Product  Time
6578  X        ...
6574  Y
6439  X
6543  Y
6756  X
6756  X

What I want as an output is this:

ID    Product   Number_of_ID_per_Product  Time
6578  X         1                         ...
6574  Y         1
6439  X         2
6543  Y         2
6756  X         3
6756  X         4

I tried

df['ID_Number_per_Part']=vormessen.groupby(['Product'])['ID'].count()

which gives me only NaN values.

Michelle
  • 247
  • 2
  • 9

1 Answers1

0

this can be done using group by statement (a concept very widely used in SQL)

df[0].groupyby(1: name of the field to group by with)(2: aggregated fields).([3: type of aggregation].()
    

In your case it will be:

  1. Vormasseen
  2. Product
  3. ID
  4. Count

Similarly, you can do the same for avg, max, min, etc.

trillion
  • 1,207
  • 1
  • 5
  • 15