Issue
I would like to sort the data by the month however not alphabetically rather by the month order, i.e first the sales for January and then February etc, see the illustrated data I created
month=['January','February','March','April','January','February','March','April']
sales=[10,100,130,145,13409,670,560,40]
dict = {'month': month, 'sales': sales}
df = pd.DataFrame(dict)
df.groupby('month')['sales'].mean().sort_values()
in this case I received data by the sales average, however I would like to sort the value by the month order
Solution
Alternatively and probably slower, but just for posterity you can use lambda with map:
month_order = {"January": 1, "February": 2, "March": 3, "April": 4}
df = df.sort_values(by="month", key=lambda x: x.map(month_order), ignore_index=True)
Answered By - Jason Baker
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.