Issue
I have this dataframe of sales:
Date datetime64[ns]
Day int64
Month object
Year int64
Customer_Age int64
Age_Group object
Customer_Gender object
Country object
State object
Product_Category object
Sub_Category object
Product object
Order_Quantity int64
Unit_Cost int64
Unit_Price int64
Profit int64
Cost int64
Revenue int64
dtype: object
When I try to plot the quantity of sales per month:
sales["Month"].value_counts().plot(kind="bar")
I get this chart in which months are not ordered chronologically, they are ordered by quantity instead. How can I do that?
I also tried with "sort=False" as parameter:
sales["Month"].value_counts(sort=False).plot(kind="bar")
but I get this chart.
Thanks!
Solution
- have generated some sample data
reindex()
value_counts()
series in order you want.
sales = pd.DataFrame({"Month": ["December", "November", "December", "June", "May", "October", "October", "October", "August", "March", "August", "June", "January", "December", "May", "January", "February", "March", "April", "April", "November", "October", "January", "July", "April", "February", "March", "March", "April", "January", "April", "January", "August", "March", "March", "May", "March", "January", "May", "November", "October", "January", "October", "February", "August", "December", "June", "August", "April", "August", "May", "December", "June", "April", "March", "August", "February", "October", "November", "July", "December", "April", "May", "July", "October", "June", "March", "May", "February", "September", "September", "March", "September", "April", "September", "January", "May", "November", "June", "December", "February", "January", "July", "January", "November", "October", "August", "December", "August", "August", "January", "August", "February", "April", "August", "March", "December", "May", "June", "July", "February", "September", "November", "January", "November", "May", "January", "July", "June", "June", "April", "January", "March", "May", "December", "August", "August", "March", "April", "June", "November", "February", "August", "March", "November", "June", "June", "May", "April", "December", "June", "December", "November", "March", "November", "June", "October", "October", "March", "August", "September", "October", "June", "January", "January", "October", "July", "March", "September", "December", "December", "March", "December", "November", "April", "March", "July", "September", "January", "May", "February", "May", "April", "April", "December", "November", "December", "October", "August", "January", "June", "March", "May", "September", "November", "November", "July", "November", "September", "May", "April", "January", "October", "February", "April", "April", "May", "January", "January", "December", "February", "March", "February", "September", "October", "April", "November", "April", "August", "May"]})
sales["Month"].value_counts(sort=False).reindex(
pd.Series(pd.date_range("1-jan-2021", freq="M", periods=12)).dt.strftime("%B")
).plot(kind="bar")
Answered By - Rob Raymond
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.