How to Number Groups in Pandas

Asked 2 years ago, Updated 2 years ago, 39 views

I would like to know how to add serial numbers for each combination of rows in Pandas dataframe.
Specifically, I would like to create a column called "Count" for serial numbers for each combination of "ID", "Date", and "Store".

If you have an ID, a date, or a store, the value of the count containing the serial number is as follows:

df=pd.DataFrame({'ID': [1,1,2,2,2,3,3,3,4,4,4],
Day: ['4/3', '4/5', '4/1', '4/1', '5/30', '5/5', '5/5', '5/5', '6/7', '5/3', '5/3', '6/7', '5/3', '6/7',
'Store': ['A', 'B', 'A', 'C', 'B', 'B', 'B', 'C', 'D', 'A', 'A',
'Count': ['1', '2', '1', '2', '2', '2', '2', '3', '1', '2', '2', '3']})

I would appreciate it if you could give me advice if you know.

python pandas

2022-09-29 22:39

1 Answers

Summarize using the DataFrame.groupby method and then number using group.group_info.

The prerequisite is that the target data frames are sorted by ID, sorted by day within the same ID, sorted by store in the same ID, and sorted by store in the same ID, day, and store.Otherwise, you will get the wrong result.

>>ids=df.groupby(['ID'].group_info[0]
>>ids
array([0,0,1,1,1,2,2,2,2,3,3,3])
>>comb=df.groupby(['ID', 'Day', 'Store'].group_info[0]
>>comb
array([0,1,2,2,3,4,5,5,6,7,8,9])

>>> count = [ ];nth = 0
>>> for i, cin enumerate (comb):
      if i==0 or ids[i]!=ids[i-1]:
        nth = 1
      elifc!=comb[i-1]:
        nth + = 1
      count+=[nth]

>>>df ['count'] = count
>>df
    ID Date Store Count
0 14/3 A1
1 4/5 B2
2 4/1 A1
3 2 4/1 A1
42 5/30 C2
5 3 5/5 A1
635/5B2
7 35/5 B2
836/7 B3
945/3 C1
10 45/3 D2
114 6/7 A3


2022-09-29 22:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.