'For every row with the same name, find the 5 lowest number Pandas Python
My data frame looks like this
Location week Number
Austria 1 154
Austria 2 140
Belgium 1 139
Bulgaria 2 110
Bulgaria 1 164
the solution should look like this
Location week Number
Austria 3 100
Austria 2 101
Austria 1 102
Bulgaria 2 100
Bulgaria 3 101
Bulgaria 1 102
this means that I need to display
- Column 1 : I need to group the countries by name
- Column 2 : Week (every country has 53 weeks assigned to them)
- Column 3 : Show the numbers that occured in each of 53 weeks in an ascending order
I can not get my head around this
Solution 1:[1]
another way using .cumcount() and .loc
con = df.sort_values('Number',ascending=True).groupby('Location').cumcount()
df.loc[con.lt(5)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Umar.H |
