I have a sample dataframe like this. Basically I want to do ranking based on item_number and location_id. I could've done something like that in SQL using window function (dense_rank, over() partition by).
df = pd.DataFrame({'item_number': [1029980, 1029980, 1029980, 1029980, 1029980,
1029980, 1029980, 1029980, 1029980, 1029980],
'location_id': ['L3-25-AA-05-B', 'L3-25-AA-05-B', 'L3-25-AA-05-B', 'L3-25-AA-05-B', 'L3-25-AA-05-B',
'L4-25-AA-05-B', 'L4-25-AA-05-B','L4-25-AA-05-B', 'L4-25-AA-05-B', 'L4-25-AA-05-B'],
'Date': ['2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04', '2021-10-05',
'2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04', '2021-10-05']})
item_number | location_id | Date |
---|---|---|
1029980 | L3-25-AA-05-B | 2021-10-01 |
1029980 | L3-25-AA-05-B | 2021-10-02 |
1029980 | L3-25-AA-05-B | 2021-10-03 |
1029980 | L3-25-AA-05-B | 2021-10-04 |
1029980 | L3-25-AA-05-B | 2021-10-05 |
1029980 | L4-25-AA-05-B | 2021-10-01 |
1029980 | L4-25-AA-05-B | 2021-10-02 |
1029980 | L4-25-AA-05-B | 2021-10-03 |
1029980 | L4-25-AA-05-B | 2021-10-04 |
1029980 | L4-25-AA-05-B | 2021-10-05 |
I want the data to be like this. The ranking is grouped by item_number and location_id. If the item_number and location_id are same then it's considered in a same group and ranking should be done based on the Date.
item_number | location_id | Date | Rank |
---|---|---|---|
1029980 | L3-25-AA-05-B | 2021-10-01 | 5 |
1029980 | L3-25-AA-05-B | 2021-10-02 | 4 |
1029980 | L3-25-AA-05-B | 2021-10-03 | 3 |
1029980 | L3-25-AA-05-B | 2021-10-04 | 2 |
1029980 | L3-25-AA-05-B | 2021-10-05 | 1 |
1029980 | L4-25-AA-05-B | 2021-10-01 | 5 |
1029980 | L4-25-AA-05-B | 2021-10-02 | 4 |
1029980 | L4-25-AA-05-B | 2021-10-03 | 3 |
1029980 | L4-25-AA-05-B | 2021-10-04 | 2 |
1029980 | L4-25-AA-05-B | 2021-10-05 | 1 |
I have tried this code but it's giving an error as the columns are all string.
test['rank'] = test.groupby(['item_number','location_id']).rank()
The above code gave me this error.
DataError: No numeric types to aggregate
Can anyone help me in this regard please?