Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,24 +5,15 @@ import numpy as np
|
|
| 5 |
# 设置页面布局为宽屏模式,并设置页面标题
|
| 6 |
st.set_page_config(layout="wide", page_title="影城效率分析 - 最终版")
|
| 7 |
|
| 8 |
-
|
| 9 |
def clean_movie_title(title):
|
| 10 |
"""
|
| 11 |
-
|
|
|
|
| 12 |
"""
|
| 13 |
if not isinstance(title, str):
|
| 14 |
return title
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
'国语', '英语', '粤语', '日语', '原版', '修复版',
|
| 18 |
-
'(国)', '(英)', '(粤)'
|
| 19 |
-
]
|
| 20 |
-
parts = title.split()
|
| 21 |
-
cleaned_parts = [p for p in parts if p.upper() not in [s.upper() for s in suffixes_to_remove]]
|
| 22 |
-
if not cleaned_parts:
|
| 23 |
-
return title
|
| 24 |
-
return ' '.join(cleaned_parts).strip()
|
| 25 |
-
|
| 26 |
|
| 27 |
def style_efficiency(row):
|
| 28 |
"""
|
|
@@ -34,11 +25,10 @@ def style_efficiency(row):
|
|
| 34 |
seat_efficiency = row.get('座次效率', 0)
|
| 35 |
session_efficiency = row.get('场次效率', 0)
|
| 36 |
if (seat_efficiency < 0.5 or seat_efficiency > 1.5 or
|
| 37 |
-
|
| 38 |
return [highlight] * len(row)
|
| 39 |
return [default] * len(row)
|
| 40 |
|
| 41 |
-
|
| 42 |
def process_and_analyze_data(df):
|
| 43 |
"""
|
| 44 |
核心数据处理与分析函数。
|
|
@@ -53,7 +43,7 @@ def process_and_analyze_data(df):
|
|
| 53 |
人次=('总人次', 'sum')
|
| 54 |
).reset_index()
|
| 55 |
analysis_df.rename(columns={'影片名称_清理后': '影片'}, inplace=True)
|
| 56 |
-
|
| 57 |
analysis_df = analysis_df.sort_values(by='票房', ascending=False).reset_index(drop=True)
|
| 58 |
|
| 59 |
total_seats = analysis_df['座位数'].sum()
|
|
@@ -66,28 +56,26 @@ def process_and_analyze_data(df):
|
|
| 66 |
analysis_df['票房比'] = np.divide(analysis_df['票房'], total_revenue).fillna(0)
|
| 67 |
analysis_df['座次效率'] = np.divide(analysis_df['票房比'], analysis_df['座次比']).fillna(0)
|
| 68 |
analysis_df['场次效率'] = np.divide(analysis_df['票房比'], analysis_df['场次比']).fillna(0)
|
| 69 |
-
|
| 70 |
-
# **优化1:移除“序号”列的定义**
|
| 71 |
final_columns = [
|
| 72 |
-
'影片', '座位数', '场次', '票房', '人次', '均价',
|
| 73 |
'座次比', '场次比', '票房比', '座次效率', '场次效率'
|
| 74 |
]
|
| 75 |
analysis_df = analysis_df[final_columns]
|
| 76 |
-
|
| 77 |
return analysis_df
|
| 78 |
|
| 79 |
-
|
| 80 |
# --- Streamlit 用户界面 ---
|
| 81 |
|
| 82 |
-
st.title('
|
| 83 |
-
st.write("上传 `影片映出日累计报表.xlsx`
|
| 84 |
|
| 85 |
uploaded_file = st.file_uploader("请在此处上传 Excel 文件", type=['xlsx', 'xls', 'csv'])
|
| 86 |
|
| 87 |
if uploaded_file is not None:
|
| 88 |
try:
|
| 89 |
df = pd.read_excel(uploaded_file, skiprows=3, header=None)
|
| 90 |
-
|
| 91 |
df.rename(columns={
|
| 92 |
0: '影片名称', 2: '放映时间', 5: '总人次', 6: '总收入', 7: '座位数'
|
| 93 |
}, inplace=True)
|
|
@@ -101,11 +89,11 @@ if uploaded_file is not None:
|
|
| 101 |
|
| 102 |
df['放映时间'] = pd.to_datetime(df['放映时间'], format='%H:%M:%S', errors='coerce').dt.time
|
| 103 |
df.dropna(subset=['放映时间'], inplace=True)
|
| 104 |
-
|
| 105 |
df['影片名称_清理后'] = df['影片名称'].apply(clean_movie_title)
|
| 106 |
|
| 107 |
-
st.
|
| 108 |
-
|
| 109 |
format_config = {
|
| 110 |
'座位数': '{:,.0f}', '场次': '{:,.0f}', '人次': '{:,.0f}',
|
| 111 |
'票房': '{:,.2f}', '均价': '{:.2f}', '座次比': '{:.2%}', '场次比': '{:.2%}',
|
|
@@ -113,13 +101,13 @@ if uploaded_file is not None:
|
|
| 113 |
}
|
| 114 |
|
| 115 |
# --- 1. 全天数据分析 ---
|
| 116 |
-
st.header("
|
| 117 |
-
|
|
|
|
| 118 |
full_day_analysis = process_and_analyze_data(df.copy())
|
| 119 |
-
|
| 120 |
if not full_day_analysis.empty:
|
| 121 |
table_height = (len(full_day_analysis) + 1) * 35 + 3
|
| 122 |
-
# **优化2:使用 .hide(axis="index") 隐藏默认序号列**
|
| 123 |
st.dataframe(
|
| 124 |
full_day_analysis.style.format(format_config).apply(style_efficiency, axis=1).hide(axis="index"),
|
| 125 |
height=table_height,
|
|
@@ -129,17 +117,16 @@ if uploaded_file is not None:
|
|
| 129 |
st.warning("全天数据不足,无法生成分析报告。")
|
| 130 |
|
| 131 |
# --- 2. 黄金时段数据分析 ---
|
| 132 |
-
st.header("黄金时段 (14:00 - 21:00)
|
| 133 |
-
|
| 134 |
start_time = pd.to_datetime('14:00:00').time()
|
| 135 |
end_time = pd.to_datetime('21:00:00').time()
|
| 136 |
prime_time_df = df[df['放映时间'].between(start_time, end_time)]
|
| 137 |
|
| 138 |
prime_time_analysis = process_and_analyze_data(prime_time_df.copy())
|
| 139 |
-
|
| 140 |
if not prime_time_analysis.empty:
|
| 141 |
table_height_prime = (len(prime_time_analysis) + 1) * 35 + 3
|
| 142 |
-
# **优化2:同样隐藏黄金时段表格的默认序号**
|
| 143 |
st.dataframe(
|
| 144 |
prime_time_analysis.style.format(format_config).apply(style_efficiency, axis=1).hide(axis="index"),
|
| 145 |
height=table_height_prime,
|
|
@@ -147,14 +134,15 @@ if uploaded_file is not None:
|
|
| 147 |
)
|
| 148 |
else:
|
| 149 |
st.warning("黄金时段内没有有效场次数据,无法生成分析报告。")
|
| 150 |
-
|
| 151 |
# --- 3. 一键复制影片列表 ---
|
| 152 |
if not full_day_analysis.empty:
|
| 153 |
st.header("复制当日影片列表")
|
| 154 |
-
|
|
|
|
| 155 |
movie_titles = full_day_analysis['影片'].tolist()
|
| 156 |
formatted_titles = ''.join([f'《{title}》' for title in movie_titles])
|
| 157 |
-
|
| 158 |
st.code(formatted_titles, language='text')
|
| 159 |
|
| 160 |
except Exception as e:
|
|
|
|
| 5 |
# 设置页面布局为宽屏模式,并设置页面标题
|
| 6 |
st.set_page_config(layout="wide", page_title="影城效率分析 - 最终版")
|
| 7 |
|
|
|
|
| 8 |
def clean_movie_title(title):
|
| 9 |
"""
|
| 10 |
+
清理并规范化电影标题。
|
| 11 |
+
根据用户最新指示:只保留字符串中第一个空格之前的部分。
|
| 12 |
"""
|
| 13 |
if not isinstance(title, str):
|
| 14 |
return title
|
| 15 |
+
# 将标题按第一个空格分割,并只取第一部分
|
| 16 |
+
return title.split(' ', 1)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def style_efficiency(row):
|
| 19 |
"""
|
|
|
|
| 25 |
seat_efficiency = row.get('座次效率', 0)
|
| 26 |
session_efficiency = row.get('场次效率', 0)
|
| 27 |
if (seat_efficiency < 0.5 or seat_efficiency > 1.5 or
|
| 28 |
+
session_efficiency < 0.5 or session_efficiency > 1.5):
|
| 29 |
return [highlight] * len(row)
|
| 30 |
return [default] * len(row)
|
| 31 |
|
|
|
|
| 32 |
def process_and_analyze_data(df):
|
| 33 |
"""
|
| 34 |
核心数据处理与分析函数。
|
|
|
|
| 43 |
人次=('总人次', 'sum')
|
| 44 |
).reset_index()
|
| 45 |
analysis_df.rename(columns={'影片名称_清理后': '影片'}, inplace=True)
|
| 46 |
+
|
| 47 |
analysis_df = analysis_df.sort_values(by='票房', ascending=False).reset_index(drop=True)
|
| 48 |
|
| 49 |
total_seats = analysis_df['座位数'].sum()
|
|
|
|
| 56 |
analysis_df['票房比'] = np.divide(analysis_df['票房'], total_revenue).fillna(0)
|
| 57 |
analysis_df['座次效率'] = np.divide(analysis_df['票房比'], analysis_df['座次比']).fillna(0)
|
| 58 |
analysis_df['场次效率'] = np.divide(analysis_df['票房比'], analysis_df['场次比']).fillna(0)
|
| 59 |
+
|
|
|
|
| 60 |
final_columns = [
|
| 61 |
+
'影片', '座位数', '场次', '票房', '人次', '均价',
|
| 62 |
'座次比', '场次比', '票房比', '座次效率', '场次效率'
|
| 63 |
]
|
| 64 |
analysis_df = analysis_df[final_columns]
|
| 65 |
+
|
| 66 |
return analysis_df
|
| 67 |
|
|
|
|
| 68 |
# --- Streamlit 用户界面 ---
|
| 69 |
|
| 70 |
+
st.title('自动化影城数据分析报告 (最终版)')
|
| 71 |
+
st.write("上传 `影片映出日累计报表.xlsx` 文件。此版本已采纳您所有的优化建议。")
|
| 72 |
|
| 73 |
uploaded_file = st.file_uploader("请在此处上传 Excel 文件", type=['xlsx', 'xls', 'csv'])
|
| 74 |
|
| 75 |
if uploaded_file is not None:
|
| 76 |
try:
|
| 77 |
df = pd.read_excel(uploaded_file, skiprows=3, header=None)
|
| 78 |
+
|
| 79 |
df.rename(columns={
|
| 80 |
0: '影片名称', 2: '放映时间', 5: '总人次', 6: '总收入', 7: '座位数'
|
| 81 |
}, inplace=True)
|
|
|
|
| 89 |
|
| 90 |
df['放映时间'] = pd.to_datetime(df['放映时间'], format='%H:%M:%S', errors='coerce').dt.time
|
| 91 |
df.dropna(subset=['放映时间'], inplace=True)
|
| 92 |
+
|
| 93 |
df['影片名称_清理后'] = df['影片名称'].apply(clean_movie_title)
|
| 94 |
|
| 95 |
+
st.success("文件上传成功,数据已按最终规则处理!")
|
| 96 |
+
|
| 97 |
format_config = {
|
| 98 |
'座位数': '{:,.0f}', '场次': '{:,.0f}', '人次': '{:,.0f}',
|
| 99 |
'票房': '{:,.2f}', '均价': '{:.2f}', '座次比': '{:.2%}', '场次比': '{:.2%}',
|
|
|
|
| 101 |
}
|
| 102 |
|
| 103 |
# --- 1. 全天数据分析 ---
|
| 104 |
+
st.header("全天场次效率分析 (按总收入排序)")
|
| 105 |
+
st.write("系统会自动高亮 **座次效率** 或 **场次效率** 低于 0.5 或高于 1.5 的影片。")
|
| 106 |
+
|
| 107 |
full_day_analysis = process_and_analyze_data(df.copy())
|
| 108 |
+
|
| 109 |
if not full_day_analysis.empty:
|
| 110 |
table_height = (len(full_day_analysis) + 1) * 35 + 3
|
|
|
|
| 111 |
st.dataframe(
|
| 112 |
full_day_analysis.style.format(format_config).apply(style_efficiency, axis=1).hide(axis="index"),
|
| 113 |
height=table_height,
|
|
|
|
| 117 |
st.warning("全天数据不足,无法生成分析报告。")
|
| 118 |
|
| 119 |
# --- 2. 黄金时段数据分析 ---
|
| 120 |
+
st.header("黄金时段 (14:00 - 21:00) 场次效率分析")
|
| 121 |
+
|
| 122 |
start_time = pd.to_datetime('14:00:00').time()
|
| 123 |
end_time = pd.to_datetime('21:00:00').time()
|
| 124 |
prime_time_df = df[df['放映时间'].between(start_time, end_time)]
|
| 125 |
|
| 126 |
prime_time_analysis = process_and_analyze_data(prime_time_df.copy())
|
| 127 |
+
|
| 128 |
if not prime_time_analysis.empty:
|
| 129 |
table_height_prime = (len(prime_time_analysis) + 1) * 35 + 3
|
|
|
|
| 130 |
st.dataframe(
|
| 131 |
prime_time_analysis.style.format(format_config).apply(style_efficiency, axis=1).hide(axis="index"),
|
| 132 |
height=table_height_prime,
|
|
|
|
| 134 |
)
|
| 135 |
else:
|
| 136 |
st.warning("黄金时段内没有有效场次数据,无法生成分析报告。")
|
| 137 |
+
|
| 138 |
# --- 3. 一键复制影片列表 ---
|
| 139 |
if not full_day_analysis.empty:
|
| 140 |
st.header("复制当日影片列表")
|
| 141 |
+
st.write("以下是根据全天数据生成的影片列表,已为您格式化,可直接复制使用。")
|
| 142 |
+
|
| 143 |
movie_titles = full_day_analysis['影片'].tolist()
|
| 144 |
formatted_titles = ''.join([f'《{title}》' for title in movie_titles])
|
| 145 |
+
|
| 146 |
st.code(formatted_titles, language='text')
|
| 147 |
|
| 148 |
except Exception as e:
|