Question: I need to pull the most profitable lines by quarter and month. I have the code for pulling the month, but not quarter. How would
I need to pull the most profitable lines by quarter and month. I have the code for pulling the month, but not quarter. How would I pull the data by quarter?
Data:
| Row ID | Order ID | Order Date | Year | Quarter | Ship Date | Ship Mode | Customer ID | Customer Name | Segment | Country | City | State | Postal Code | Region | Product ID | Category | Sub-Category | Product Name | Sales | Quantity | Discount | Profit |
| 1 | CA-2016-152156 | 11/8/2016 | 2016 | Q4 | 11/11/2016 | Second Class | CG-12520 | Claire Gute | Consumer | United States | Henderson | Kentucky | 42420 | South | FUR-BO-10001798 | Furniture | Bookcases | Bush Somerset Collection Bookcase | 261.96 | 2 | 0 | 41.9136 |
| 2 | CA-2016-152156 | 11/8/2016 | 2016 | Q4 | 11/11/2016 | Second Class | CG-12520 | Claire Gute | Consumer | United States | Henderson | Kentucky | 42420 | South | FUR-CH-10000454 | Furniture | Chairs | Hon Deluxe Fabric Upholstered Stacking Chairs, Rounded Back | 731.94 | 3 | 0 | 219.582 |
| 3 | CA-2016-138688 | 6/12/2016 | 2016 | Q2 | 6/16/2016 | Second Class | DV-13045 | Darrin Van Huff | Corporate | United States | Los Angeles | California | 90036 | West | OFF-LA-10000240 | Office Supplies | Labels | Self-Adhesive Address Labels for Typewriters by Universal | 14.62 | 2 | 0 | 6.8714 |
| 4 | US-2015-108966 | 10/11/2015 | 2015 | Q3 | 10/18/2015 | Standard Class | SO-20335 | Sean O'Donnell | Consumer | United States | Fort Lauderdale | Florida | 33311 | South | FUR-TA-10000577 | Furniture | Tables | Bretford CR4500 Series Slim Rectangular Table | 957.5775 | 5 | 0.45 | -383.031 |
| 5 | US-2015-108966 | 10/11/2015 | 2015 | Q4 | 10/18/2015 | Standard Class | SO-20335 | Sean O'Donnell | Consumer | United States | Fort Lauderdale | Florida | 33311 | South | OFF-ST-10000760 | Office Supplies | Storage | Eldon Fold 'N Roll Cart System | 22.368 | 2 | 0.2 | 2.5164 |
Code:
import pandas as pd
xl = pd.ExcelFile("SalesDataFull.xlsx")
#import sqlite3
def top():
OrdersOnlyData = xl.parse("Orders")
df_month = OrdersOnlyData["Order Date"].dt.month
OrdersOnlyData["Month"] = df_month
df_year = OrdersOnlyData["Order Date"].dt.year
OrdersOnlyData["Year"] = df_year
month_profit_quant = OrdersOnlyData[['Year', 'Month', 'Product Name', 'Profit']]
month_profit_sum = month_profit_quant.groupby(['Year', 'Month', 'Product Name']).sum().sort_values(by='Profit',
ascending=False)
month_profit_sum = month_profit_sum.reset_index()
month_profit_sum = month_profit_sum.sort_values(by=['Profit', 'Year', 'Month'],
ascending=False)
yr = int(input("Enter year (2014-2017): "))
while yr > 2017 or yr < 2014:
print("Invalid year. Please enter between 2014 to 2017")
yr = int(input("Enter year (2014-2017): "))
choice = input("Enter 1 for quarters or 2 for Months: ")
if choice == "1":
print("Enter 1 for Q1 ")
print("Enter 2 for Q2 ")
print("Enter 3 for Q3 ")
print("Enter 4 for Q4 ")
selection = input("Enter quarter number: ")
if selection == "1":
print("Hello")
elif selection == "2":
print()
elif selection == "3":
print()
elif selection == "4":
print()
elif choice == "2":
mth = int(input("Enter month (1-12): "))
while mth > 12 or mth < 1:
print("Invalid Month")
mth = int(input("Enter month (1-12): "))
select_year = month_profit_sum.loc[month_profit_sum['Year'] == yr]
print("The 10 most profitable products ")
print(select_year.loc[month_profit_sum['Month'] == mth].head(10))
else:
print("Invalid input only use integers 1 and 2")
def least():
OrdersOnlyData = xl.parse("Orders")
df_month = OrdersOnlyData["Order Date"].dt.month
OrdersOnlyData["Month"] = df_month
df_year = OrdersOnlyData["Order Date"].dt.year
OrdersOnlyData["Year"] = df_year
month_profit_quant = OrdersOnlyData[['Year', 'Month', 'Product Name', 'Profit']]
month_profit_sum = month_profit_quant.groupby(['Year', 'Month', 'Product Name']).sum().sort_values(by='Profit',
ascending=True)
month_profit_sum = month_profit_sum.reset_index()
month_profit_sum = month_profit_sum.sort_values(by=['Profit', 'Year', 'Month'],
ascending=True)
yr = int(input("Enter year (2014-2017): "))
while yr > 2017 or yr < 2014:
print("Invalid year. Please enter between 2014 to 2017")
yr = int(input("Enter year(2014-2017): "))
choice = input("Enter 1 for quaters or 2 for Months: ")
if choice == "1":
print("Enter 1 for Q1 ")
print("Enter 2 for Q2 ")
print("Enter 3 for Q3 ")
print("Enter 4 for Q4 ")
selection = input("Enter quarter number: ")
if selection == "1":
print("Hello")
elif selection == "2":
print()
elif selection == "3":
print()
elif selection == "4":
print()
elif choice == "2":
mth = int(input("Enter month (1-12): "))
while mth > 12 or mth < 1:
print("Invalid Month")
mth = int(input("Enter month (1-12): "))
select_year = month_profit_sum.loc[month_profit_sum['Year'] == yr]
print("The 10 most profitable products ")
print(select_year.loc[month_profit_sum['Month'] == mth].head(10))
else:
print("Invalid input only use integers 1 and 2")
def menu():
print("Find top 10 profitable products in selected year & month - Enter 1")
print("Find least 10 profitable products in selected year & month - Enter 2")
print("Exit - Enter 3")
while True:
try:
choice = int(input("Enter a choice: "))
if choice == 1:
top()
elif choice == 2:
least()
elif choice == 3:
break
else:
print("Please enter number between 1-3")
menu()
except ValueError:
print("Please enter number between 1-3")
exit
menu()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
