Spaces:
Build error
Build error
Pragya Jatav commited on
Commit ·
6ef309a
1
Parent(s): d458fbe
m1
Browse files- Streamlit_functions.py +163 -81
- __pycache__/Streamlit_functions.cpython-310.pyc +0 -0
- pages/2_Scenario_Planner.py +8 -6
- summary_df.pkl +1 -1
Streamlit_functions.py
CHANGED
|
@@ -1415,6 +1415,16 @@ def model_metrics_table_func():
|
|
| 1415 |
# model_metrics_df2 = model_metrics_df2.reset_index(drop = True)
|
| 1416 |
return model_metrics_df2
|
| 1417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1418 |
|
| 1419 |
def scenario_spend_forecasting(delta_df,start_date,end_date):
|
| 1420 |
|
|
@@ -1453,30 +1463,104 @@ def scenario_spend_forecasting(delta_df,start_date,end_date):
|
|
| 1453 |
end_date = pd.to_datetime(end_date)
|
| 1454 |
|
| 1455 |
cur_data = df[(df['Date'] >= start_date) & (df['Date'] <= end_date)]
|
| 1456 |
-
cur_data = cur_data[
|
| 1457 |
-
cur_data
|
| 1458 |
-
|
| 1459 |
-
|
| 1460 |
-
|
| 1461 |
-
|
| 1462 |
-
|
| 1463 |
-
|
| 1464 |
-
|
| 1465 |
-
|
| 1466 |
-
|
| 1467 |
-
|
| 1468 |
-
|
| 1469 |
-
|
| 1470 |
-
|
| 1471 |
-
|
| 1472 |
-
|
| 1473 |
-
|
| 1474 |
-
|
| 1475 |
-
|
| 1476 |
-
|
| 1477 |
-
|
| 1478 |
-
|
| 1479 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1480 |
|
| 1481 |
def scenario_spend_forecasting2(delta_df,start_date,end_date):
|
| 1482 |
|
|
@@ -1510,68 +1594,66 @@ def scenario_spend_forecasting2(delta_df,start_date,end_date):
|
|
| 1510 |
"DIGITAL PARTNERS"
|
| 1511 |
]
|
| 1512 |
|
| 1513 |
-
import math
|
| 1514 |
-
start_date = pd.to_datetime(start_date)
|
| 1515 |
-
end_date = pd.to_datetime(end_date)
|
| 1516 |
|
| 1517 |
-
cur_data = df[(df['Date'] >= start_date) & (df['Date'] < end_date)]
|
| 1518 |
-
cur_data = cur_data[spend_cols2]
|
| 1519 |
-
cur_data.columns = channels2
|
| 1520 |
|
| 1521 |
-
cur_data["Date2"] = cur_data["Date"]+ pd.Timedelta(days=6)
|
| 1522 |
-
cur_data["Month"] = cur_data["Date"].dt.month
|
| 1523 |
|
| 1524 |
|
| 1525 |
-
# cur_data["Date"] = delta_df["Date"]
|
| 1526 |
-
# cur_data["Date_diff"] = (cur_data["Date"]-start_date).dt.days
|
| 1527 |
-
# cur_data["Date_diff_months"] =(np.ceil(cur_data["Date_diff"] / 30))
|
| 1528 |
-
|
| 1529 |
-
data2 = cur_data.groupby("Month").agg({
|
| 1530 |
-
|
| 1531 |
-
|
| 1532 |
-
|
| 1533 |
-
|
| 1534 |
-
|
| 1535 |
-
|
| 1536 |
-
|
| 1537 |
-
|
| 1538 |
-
|
| 1539 |
-
|
| 1540 |
-
|
| 1541 |
-
|
| 1542 |
-
|
| 1543 |
-
|
| 1544 |
-
|
| 1545 |
-
|
| 1546 |
-
|
| 1547 |
-
def get_month_name(month_number):
|
| 1548 |
|
| 1549 |
-
|
| 1550 |
-
|
| 1551 |
-
|
| 1552 |
-
|
| 1553 |
-
|
| 1554 |
-
|
| 1555 |
|
| 1556 |
-
data2["Month year"] = data2["Month"].apply(get_month_name) + ' ' +(data2["Date"].dt.year+1).astype(str)
|
| 1557 |
-
# # print(data2.columns)
|
| 1558 |
-
data2 = data2[['Month year' ,'BROADCAST TV', 'CABLE TV',
|
| 1559 |
-
|
| 1560 |
-
|
| 1561 |
-
|
| 1562 |
-
|
| 1563 |
-
data2.columns = ['Month ','BROADCAST TV', 'CABLE TV',
|
| 1564 |
-
|
| 1565 |
-
|
| 1566 |
-
|
| 1567 |
-
|
| 1568 |
|
| 1569 |
-
data2.set_index('Month ', inplace=True)
|
| 1570 |
-
for c in ['BROADCAST TV', 'CABLE TV',
|
| 1571 |
-
|
| 1572 |
-
|
| 1573 |
-
|
| 1574 |
-
|
| 1575 |
-
|
| 1576 |
-
return
|
| 1577 |
|
|
|
|
| 1415 |
# model_metrics_df2 = model_metrics_df2.reset_index(drop = True)
|
| 1416 |
return model_metrics_df2
|
| 1417 |
|
| 1418 |
+
def get_month_name(month_number):
|
| 1419 |
+
|
| 1420 |
+
months = ["January", "February", "March", "April", "May", "June",
|
| 1421 |
+
"July", "August", "September", "October", "November", "December"]
|
| 1422 |
+
if 1 <= month_number <= 12:
|
| 1423 |
+
return months[month_number - 1]
|
| 1424 |
+
else:
|
| 1425 |
+
return "Invalid month number"
|
| 1426 |
+
|
| 1427 |
+
|
| 1428 |
|
| 1429 |
def scenario_spend_forecasting(delta_df,start_date,end_date):
|
| 1430 |
|
|
|
|
| 1463 |
end_date = pd.to_datetime(end_date)
|
| 1464 |
|
| 1465 |
cur_data = df[(df['Date'] >= start_date) & (df['Date'] <= end_date)]
|
| 1466 |
+
cur_data["Month"] = cur_data["Date"].dt.month
|
| 1467 |
+
# cur_data["Year"] = cur_data["Date"].dt.year
|
| 1468 |
+
cur_data["Month year"] = cur_data["Month"].apply(get_month_name) + ' ' +(cur_data["Date"].dt.year+1).astype(str)
|
| 1469 |
+
grp_cols = ['tv_broadcast_spend',
|
| 1470 |
+
'tv_cable_spend',
|
| 1471 |
+
'stream_video_spend',
|
| 1472 |
+
'olv_spend',
|
| 1473 |
+
'disp_prospect_spend',
|
| 1474 |
+
'disp_retarget_spend',
|
| 1475 |
+
'social_prospect_spend',
|
| 1476 |
+
'social_retarget_spend',
|
| 1477 |
+
'search_brand_spend',
|
| 1478 |
+
'search_nonbrand_spend',
|
| 1479 |
+
'cm_spend',
|
| 1480 |
+
'audio_spend',
|
| 1481 |
+
'email_spend',
|
| 1482 |
+
"Month",
|
| 1483 |
+
"Month year"]
|
| 1484 |
+
|
| 1485 |
+
data2 = cur_data[grp_cols].groupby("Month year").sum()
|
| 1486 |
+
|
| 1487 |
+
data2.columns = [
|
| 1488 |
+
'BROADCAST TV',
|
| 1489 |
+
'CABLE TV',
|
| 1490 |
+
'CONNECTED & OTT TV',
|
| 1491 |
+
'VIDEO',
|
| 1492 |
+
'DISPLAY PROSPECTING',
|
| 1493 |
+
'DISPLAY RETARGETING',
|
| 1494 |
+
'SOCIAL PROSPECTING',
|
| 1495 |
+
'SOCIAL RETARGETING',
|
| 1496 |
+
'SEARCH BRAND',
|
| 1497 |
+
'SEARCH NON-BRAND',
|
| 1498 |
+
'DIGITAL PARTNERS',
|
| 1499 |
+
'AUDIO',
|
| 1500 |
+
'EMAIL',
|
| 1501 |
+
"Month"]
|
| 1502 |
+
data2 = data2.sort_values("Month")
|
| 1503 |
+
data2.drop(columns = ["Month"], inplace = True)
|
| 1504 |
+
|
| 1505 |
+
key_df = pd.DataFrame()
|
| 1506 |
+
key_df["Channel_name"] = ["Email","DisplayRetargeting","\xa0Video","BroadcastTV","SocialRetargeting","Connected&OTTTV","SearchBrand","Audio","SocialProspecting","CableTV","DisplayProspecting","SearchNon-brand","DigitalPartners"]
|
| 1507 |
+
key_df["Channels"] = ["EMAIL","DISPLAY RETARGETING","VIDEO","BROADCAST TV","SOCIAL RETARGETING","CONNECTED & OTT TV","SEARCH BRAND","AUDIO","SOCIAL PROSPECTING","CABLE TV","DISPLAY PROSPECTING","SEARCH NON-BRAND","DIGITAL PARTNERS"]
|
| 1508 |
+
delta_df = delta_df.merge(key_df,on = "Channel_name",how = "inner")
|
| 1509 |
+
print(delta_df)
|
| 1510 |
+
|
| 1511 |
+
data3 = data2.copy()
|
| 1512 |
+
for channel in delta_df["Channels"]:
|
| 1513 |
+
# print(channel)
|
| 1514 |
+
delta_percent = delta_df[delta_df["Channels"]==channel]["Delta_percent"].iloc[0]
|
| 1515 |
+
print(delta_percent)
|
| 1516 |
+
data3[channel] = data3[channel]*(1+delta_percent/100)
|
| 1517 |
+
print(data2)
|
| 1518 |
+
print(data3)
|
| 1519 |
+
|
| 1520 |
+
|
| 1521 |
+
###### output dataframes
|
| 1522 |
+
output_df2 = data3.copy()
|
| 1523 |
+
|
| 1524 |
+
#### percent change dataframe
|
| 1525 |
+
delta_df2 = pd.DataFrame(data = delta_df["Delta_percent"].values,index = delta_df["Channels"])
|
| 1526 |
+
print(delta_df2)
|
| 1527 |
+
output_df1 = (pd.DataFrame(data2.sum()).transpose()).append(pd.DataFrame(data3.sum()).transpose()).append(delta_df2.transpose())
|
| 1528 |
+
output_df1.index = ["Last Year Spends", "Forecasted Spends","Spends Change"]
|
| 1529 |
+
|
| 1530 |
+
|
| 1531 |
+
|
| 1532 |
+
print(output_df1)
|
| 1533 |
+
|
| 1534 |
+
|
| 1535 |
+
|
| 1536 |
+
|
| 1537 |
+
# print (data3)
|
| 1538 |
+
# data3 = data2.append(key_df)
|
| 1539 |
+
# print (data2)
|
| 1540 |
+
# cur_data = cur_data[spend_cols]
|
| 1541 |
+
# cur_data.columns = channels
|
| 1542 |
+
# data1 = pd.DataFrame(cur_data[channels].sum().transpose()).reset_index()
|
| 1543 |
+
# data1.columns = ["Channels","last_year_spends"]
|
| 1544 |
+
|
| 1545 |
+
# df_modified = delta_df.merge(key_df,on = "Channel_name",how = "inner")
|
| 1546 |
+
# df_modified2 = df_modified.merge(data1,on = "Channels",how ="outer")
|
| 1547 |
+
# # df_modified2["Forecasted Spends"] =( df_modified2["last_year_spends"]*(1+df_modified2["Delta_percent"]/100)).astype(int)
|
| 1548 |
+
# df_modified2["Forecasted Spends"] =( df_modified2["last_year_spends"]*(1+df_modified2["Delta_percent"]/100)).apply(lambda x: "${:,.0f}".format(x))
|
| 1549 |
+
# df_modified2.index = df_modified2["Channels"]
|
| 1550 |
+
# df_modified2["Spend Change"] = (df_modified2["Delta_percent"]/100).apply(lambda x: "{:.0%}".format(x))
|
| 1551 |
+
# # df_modified2["Forecasted Spends"] = df_modified2["Forecasted Spends"].astype(int)
|
| 1552 |
+
# df_modified2["Last Year Spends"] = df_modified2["last_year_spends"].apply(lambda x: "${:,.0f}".format(x))
|
| 1553 |
+
# df_modified3 = df_modified2[["Last Year Spends","Forecasted Spends","Spend Change"]].transpose()
|
| 1554 |
+
# # df_modified2["forecasted_spends"] =
|
| 1555 |
+
# # # df_modified = delta_percent
|
| 1556 |
+
# # # df_modified["Optimised Spends"] = df_modified["Current Spends"]*
|
| 1557 |
+
# df_modified3 = df_modified3[['BROADCAST TV', 'CABLE TV',
|
| 1558 |
+
# 'CONNECTED & OTT TV', 'VIDEO', 'DISPLAY PROSPECTING',
|
| 1559 |
+
# 'DISPLAY RETARGETING', 'SOCIAL PROSPECTING', 'SOCIAL RETARGETING',
|
| 1560 |
+
# 'SEARCH BRAND', 'SEARCH NON-BRAND', 'DIGITAL PARTNERS', 'AUDIO',
|
| 1561 |
+
# 'EMAIL']]
|
| 1562 |
+
|
| 1563 |
+
return output_df1,output_df2
|
| 1564 |
|
| 1565 |
def scenario_spend_forecasting2(delta_df,start_date,end_date):
|
| 1566 |
|
|
|
|
| 1594 |
"DIGITAL PARTNERS"
|
| 1595 |
]
|
| 1596 |
|
| 1597 |
+
# import math
|
| 1598 |
+
# start_date = pd.to_datetime(start_date)
|
| 1599 |
+
# end_date = pd.to_datetime(end_date)
|
| 1600 |
|
| 1601 |
+
# cur_data = df[(df['Date'] >= start_date) & (df['Date'] < end_date)]
|
| 1602 |
+
# cur_data = cur_data[spend_cols2]
|
| 1603 |
+
# cur_data.columns = channels2
|
| 1604 |
|
| 1605 |
+
# cur_data["Date2"] = cur_data["Date"]+ pd.Timedelta(days=6)
|
| 1606 |
+
# cur_data["Month"] = cur_data["Date"].dt.month
|
| 1607 |
|
| 1608 |
|
| 1609 |
+
# # cur_data["Date"] = delta_df["Date"]
|
| 1610 |
+
# # cur_data["Date_diff"] = (cur_data["Date"]-start_date).dt.days
|
| 1611 |
+
# # cur_data["Date_diff_months"] =(np.ceil(cur_data["Date_diff"] / 30))
|
| 1612 |
+
|
| 1613 |
+
# data2 = cur_data.groupby("Month").agg({
|
| 1614 |
+
# 'BROADCAST TV':"sum",
|
| 1615 |
+
# 'CABLE TV':"sum",
|
| 1616 |
+
# 'CONNECTED & OTT TV':"sum",
|
| 1617 |
+
# 'VIDEO':"sum",
|
| 1618 |
+
# 'DISPLAY PROSPECTING':"sum",
|
| 1619 |
+
# 'DISPLAY RETARGETING':"sum",
|
| 1620 |
+
# 'SOCIAL PROSPECTING':"sum",
|
| 1621 |
+
# 'SOCIAL RETARGETING':"sum",
|
| 1622 |
+
# 'SEARCH BRAND':"sum",
|
| 1623 |
+
# 'SEARCH NON-BRAND':"sum",
|
| 1624 |
+
# 'DIGITAL PARTNERS':"sum",
|
| 1625 |
+
# 'AUDIO':"sum",
|
| 1626 |
+
# 'EMAIL':"sum"
|
| 1627 |
+
# }).reset_index()
|
| 1628 |
+
|
| 1629 |
+
# def get_month_name(month_number):
|
|
|
|
|
|
|
| 1630 |
|
| 1631 |
+
# months = ["January", "February", "March", "April", "May", "June",
|
| 1632 |
+
# "July", "August", "September", "October", "November", "December"]
|
| 1633 |
+
# if 1 <= month_number <= 12:
|
| 1634 |
+
# return months[month_number - 1]
|
| 1635 |
+
# else:
|
| 1636 |
+
# return "Invalid month number"
|
| 1637 |
|
| 1638 |
+
# data2["Month year"] = data2["Month"].apply(get_month_name) + ' ' +(data2["Date"].dt.year+1).astype(str)
|
| 1639 |
+
# # # print(data2.columns)
|
| 1640 |
+
# data2 = data2[['Month year' ,'BROADCAST TV', 'CABLE TV',
|
| 1641 |
+
# 'CONNECTED & OTT TV', 'VIDEO', 'DISPLAY PROSPECTING',
|
| 1642 |
+
# 'DISPLAY RETARGETING', 'SOCIAL PROSPECTING', 'SOCIAL RETARGETING',
|
| 1643 |
+
# 'SEARCH BRAND', 'SEARCH NON-BRAND', 'DIGITAL PARTNERS', 'AUDIO',
|
| 1644 |
+
# 'EMAIL']]
|
| 1645 |
+
# data2.columns = ['Month ','BROADCAST TV', 'CABLE TV',
|
| 1646 |
+
# 'CONNECTED & OTT TV', 'VIDEO', 'DISPLAY PROSPECTING',
|
| 1647 |
+
# 'DISPLAY RETARGETING', 'SOCIAL PROSPECTING', 'SOCIAL RETARGETING',
|
| 1648 |
+
# 'SEARCH BRAND', 'SEARCH NON-BRAND', 'DIGITAL PARTNERS', 'AUDIO',
|
| 1649 |
+
# 'EMAIL']
|
| 1650 |
|
| 1651 |
+
# data2.set_index('Month ', inplace=True)
|
| 1652 |
+
# for c in ['BROADCAST TV', 'CABLE TV',
|
| 1653 |
+
# 'CONNECTED & OTT TV', 'VIDEO', 'DISPLAY PROSPECTING',
|
| 1654 |
+
# 'DISPLAY RETARGETING', 'SOCIAL PROSPECTING', 'SOCIAL RETARGETING',
|
| 1655 |
+
# 'SEARCH BRAND', 'SEARCH NON-BRAND', 'DIGITAL PARTNERS', 'AUDIO',
|
| 1656 |
+
# 'EMAIL']:
|
| 1657 |
+
# data2[c] = data2[c].apply(lambda x: "${:,.0f}".format(x))
|
| 1658 |
+
return key_df
|
| 1659 |
|
__pycache__/Streamlit_functions.cpython-310.pyc
CHANGED
|
Binary files a/__pycache__/Streamlit_functions.cpython-310.pyc and b/__pycache__/Streamlit_functions.cpython-310.pyc differ
|
|
|
pages/2_Scenario_Planner.py
CHANGED
|
@@ -2068,7 +2068,7 @@ if auth_status == True:
|
|
| 2068 |
# print(a)
|
| 2069 |
# print(summary_df_sorted['Actual_spend'].sum())
|
| 2070 |
# print(summary_df_sorted['Actual_spend'])
|
| 2071 |
-
print(col,summary_df_sorted)
|
| 2072 |
# print(summary_df_sorted['Old_sales'])
|
| 2073 |
# print(col, "old efficiency ", a)
|
| 2074 |
with bin_placeholder:
|
|
@@ -2218,18 +2218,20 @@ if auth_status == True:
|
|
| 2218 |
try:
|
| 2219 |
|
| 2220 |
st.write("Forecasted Spends wrt. Channels ")
|
| 2221 |
-
|
| 2222 |
-
|
| 2223 |
-
|
|
|
|
| 2224 |
st.dataframe(forecasted_table_df)
|
| 2225 |
|
| 2226 |
st.write("Monthly Breakdown Of Forecasted Spends wrt. Channels ")
|
|
|
|
| 2227 |
st.dataframe(forecasted_table_df2)
|
| 2228 |
|
| 2229 |
|
| 2230 |
except:
|
| 2231 |
st.warning("Please make sure the base data is updated")
|
| 2232 |
-
|
| 2233 |
def save_report_forecast(forecasted_table_df,forecasted_table_df2):
|
| 2234 |
|
| 2235 |
# Convert the DataFrame to an Excel file in memory
|
|
@@ -2252,7 +2254,7 @@ if auth_status == True:
|
|
| 2252 |
|
| 2253 |
st.download_button(
|
| 2254 |
"Download Report",
|
| 2255 |
-
data = save_report_forecast(
|
| 2256 |
file_name = report_name+".xlsx",
|
| 2257 |
mime="application/vnd.ms-excel",
|
| 2258 |
# on_click=lambda: save_report_forecast(forecasted_table_df,report_name),
|
|
|
|
| 2068 |
# print(a)
|
| 2069 |
# print(summary_df_sorted['Actual_spend'].sum())
|
| 2070 |
# print(summary_df_sorted['Actual_spend'])
|
| 2071 |
+
# print(col,summary_df_sorted)
|
| 2072 |
# print(summary_df_sorted['Old_sales'])
|
| 2073 |
# print(col, "old efficiency ", a)
|
| 2074 |
with bin_placeholder:
|
|
|
|
| 2218 |
try:
|
| 2219 |
|
| 2220 |
st.write("Forecasted Spends wrt. Channels ")
|
| 2221 |
+
output_df1, output_df2 = sf.scenario_spend_forecasting(summary_df_sorted,start_date1- relativedelta(years=1),end_date1- relativedelta(years=1))
|
| 2222 |
+
forecasted_table_df = output_df1.copy()
|
| 2223 |
+
forecasted_table_df.iloc[:2] = forecasted_table_df.iloc[:2].applymap(lambda x: "{:,.0f}".format(x))
|
| 2224 |
+
forecasted_table_df.iloc[-1] = forecasted_table_df.iloc[-1].apply(lambda x: "{:.1f}%".format(x))
|
| 2225 |
st.dataframe(forecasted_table_df)
|
| 2226 |
|
| 2227 |
st.write("Monthly Breakdown Of Forecasted Spends wrt. Channels ")
|
| 2228 |
+
forecasted_table_df2 = output_df2.applymap(lambda x: "{:,.0f}".format(x))
|
| 2229 |
st.dataframe(forecasted_table_df2)
|
| 2230 |
|
| 2231 |
|
| 2232 |
except:
|
| 2233 |
st.warning("Please make sure the base data is updated")
|
| 2234 |
+
|
| 2235 |
def save_report_forecast(forecasted_table_df,forecasted_table_df2):
|
| 2236 |
|
| 2237 |
# Convert the DataFrame to an Excel file in memory
|
|
|
|
| 2254 |
|
| 2255 |
st.download_button(
|
| 2256 |
"Download Report",
|
| 2257 |
+
data = save_report_forecast(output_df1,output_df2),
|
| 2258 |
file_name = report_name+".xlsx",
|
| 2259 |
mime="application/vnd.ms-excel",
|
| 2260 |
# on_click=lambda: save_report_forecast(forecasted_table_df,report_name),
|
summary_df.pkl
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 1822
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:89f9c3faa933ceebd7bee0c47ec71d04b7b8bdf401cd2ad55f0e22604203216f
|
| 3 |
size 1822
|