I want to extract N pieces for each group, and one of the countermeasures. For example, when you want to extract the daily column and the top nth sales.
sort_values
Sorting
groupby
Grouping
.head(n)
N from the beginning in the group
.tail()
N from behind in the group
Sample code: Up to the top 3 in sales
newdf = df.sort_values(by=['date','Sales'], ascending=[True, False]).groupby('date').head(3)
Reference article:
https://blog.shikoan.com/pandas-groupby-head/
Recommended Posts