Spaces:
Sleeping
Sleeping
abraham9486937737 commited on
Commit ·
60c2ee7
1
Parent(s): 588dcba
Fix revenue distribution chart: prioritize positive values and use Booking Amount
Browse files- streamlit_app/app.py +48 -12
streamlit_app/app.py
CHANGED
|
@@ -474,12 +474,23 @@ if page == "📊 Overview":
|
|
| 474 |
|
| 475 |
with col2:
|
| 476 |
st.subheader("Revenue Distribution")
|
|
|
|
|
|
|
|
|
|
| 477 |
revenue_candidates = [
|
| 478 |
col for col in df_filtered.columns
|
| 479 |
if any(k in col.lower() for k in ['amount', 'revenue', 'total', 'rate', 'price', 'cost'])
|
| 480 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
best_col = None
|
| 482 |
best_score = 0
|
|
|
|
| 483 |
for col in revenue_candidates:
|
| 484 |
cleaned = (
|
| 485 |
df_filtered[col]
|
|
@@ -487,8 +498,11 @@ if page == "📊 Overview":
|
|
| 487 |
.str.replace(r'[^0-9.-]', '', regex=True)
|
| 488 |
)
|
| 489 |
vals = pd.to_numeric(cleaned, errors='coerce')
|
| 490 |
-
|
| 491 |
-
|
|
|
|
|
|
|
|
|
|
| 492 |
best_score = score
|
| 493 |
best_col = col
|
| 494 |
|
|
@@ -498,16 +512,38 @@ if page == "📊 Overview":
|
|
| 498 |
.astype(str)
|
| 499 |
.str.replace(r'[^0-9.-]', '', regex=True)
|
| 500 |
)
|
| 501 |
-
revenue_values = pd.to_numeric(cleaned, errors='coerce')
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 511 |
else:
|
| 512 |
st.info("No revenue values available for the current filters.")
|
| 513 |
else:
|
|
|
|
| 474 |
|
| 475 |
with col2:
|
| 476 |
st.subheader("Revenue Distribution")
|
| 477 |
+
|
| 478 |
+
# Prioritize certain columns for revenue
|
| 479 |
+
priority_cols = ['Total Amount', 'Booking Amount', 'Received Amount']
|
| 480 |
revenue_candidates = [
|
| 481 |
col for col in df_filtered.columns
|
| 482 |
if any(k in col.lower() for k in ['amount', 'revenue', 'total', 'rate', 'price', 'cost'])
|
| 483 |
]
|
| 484 |
+
|
| 485 |
+
# Sort by priority
|
| 486 |
+
revenue_candidates = sorted(
|
| 487 |
+
revenue_candidates,
|
| 488 |
+
key=lambda x: (priority_cols.index(x) if x in priority_cols else len(priority_cols))
|
| 489 |
+
)
|
| 490 |
+
|
| 491 |
best_col = None
|
| 492 |
best_score = 0
|
| 493 |
+
|
| 494 |
for col in revenue_candidates:
|
| 495 |
cleaned = (
|
| 496 |
df_filtered[col]
|
|
|
|
| 498 |
.str.replace(r'[^0-9.-]', '', regex=True)
|
| 499 |
)
|
| 500 |
vals = pd.to_numeric(cleaned, errors='coerce')
|
| 501 |
+
# Filter to positive values only (revenue should be > 0)
|
| 502 |
+
positive_vals = vals[vals > 0]
|
| 503 |
+
score = len(positive_vals)
|
| 504 |
+
|
| 505 |
+
if score > best_score and positive_vals.sum() > 0:
|
| 506 |
best_score = score
|
| 507 |
best_col = col
|
| 508 |
|
|
|
|
| 512 |
.astype(str)
|
| 513 |
.str.replace(r'[^0-9.-]', '', regex=True)
|
| 514 |
)
|
| 515 |
+
revenue_values = pd.to_numeric(cleaned, errors='coerce')
|
| 516 |
+
# Filter to positive values only
|
| 517 |
+
revenue_values = revenue_values[revenue_values > 0].dropna()
|
| 518 |
+
|
| 519 |
+
if not revenue_values.empty and len(revenue_values) > 5:
|
| 520 |
+
try:
|
| 521 |
+
# Create histogram
|
| 522 |
+
fig = px.histogram(
|
| 523 |
+
revenue_values,
|
| 524 |
+
nbins=min(30, len(revenue_values)//5),
|
| 525 |
+
title=f"Revenue Distribution - {best_col}",
|
| 526 |
+
color_discrete_sequence=['#636EFA'],
|
| 527 |
+
labels={'value': best_col, 'count': 'Count'}
|
| 528 |
+
)
|
| 529 |
+
fig.update_layout(
|
| 530 |
+
height=400,
|
| 531 |
+
xaxis_title=best_col,
|
| 532 |
+
yaxis_title="Count",
|
| 533 |
+
hovermode='x unified'
|
| 534 |
+
)
|
| 535 |
+
fig.update_traces(marker=dict(line=dict(width=0)))
|
| 536 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 537 |
+
except Exception as e:
|
| 538 |
+
st.warning(f"Could not create histogram: {e}")
|
| 539 |
+
# Fallback: show summary stats
|
| 540 |
+
st.metric("Average Revenue", f"₹{revenue_values.mean():,.0f}")
|
| 541 |
+
st.metric("Median Revenue", f"₹{revenue_values.median():,.0f}")
|
| 542 |
+
st.metric("Max Revenue", f"₹{revenue_values.max():,.0f}")
|
| 543 |
+
elif revenue_values.empty:
|
| 544 |
+
st.info(f"No positive revenue values found in {best_col}")
|
| 545 |
+
else:
|
| 546 |
+
st.info("Insufficient data for distribution chart")
|
| 547 |
else:
|
| 548 |
st.info("No revenue values available for the current filters.")
|
| 549 |
else:
|