Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,18 +4,17 @@ import re
|
|
4 |
|
5 |
st.set_page_config(layout="wide")
|
6 |
|
7 |
-
st.title('
|
8 |
|
9 |
# 1. 文件上传组件
|
10 |
uploaded_file = st.file_uploader("上传“影片放映时间表.xlsx”文件", type=['xlsx'])
|
11 |
-
ad_duration = st.number_input('输入每个广告的时长(分钟)', min_value=0, value=
|
12 |
|
13 |
if uploaded_file is not None:
|
14 |
try:
|
15 |
# 读取Excel文件
|
16 |
df = pd.read_excel(uploaded_file, header=3)
|
17 |
|
18 |
-
# --- 错误修复 ---
|
19 |
# 明确将“影片”列转换为字符串类型,以避免混合类型错误
|
20 |
df['影片'] = df['影片'].astype(str)
|
21 |
|
@@ -24,7 +23,6 @@ if uploaded_file is not None:
|
|
24 |
|
25 |
|
26 |
# 2. 数据处理和清洗
|
27 |
-
# 清洗“影厅”列
|
28 |
def clean_hall_name(name):
|
29 |
if isinstance(name, str):
|
30 |
match = re.search(r'【(\d+)号', name)
|
@@ -34,42 +32,59 @@ if uploaded_file is not None:
|
|
34 |
|
35 |
|
36 |
df['影厅'] = df['影厅'].apply(clean_hall_name)
|
37 |
-
|
38 |
-
# 将“放映日期”转换为日期时间对象
|
39 |
df['放映日期'] = pd.to_datetime(df['放映日期'])
|
40 |
df['日期'] = df['放映日期'].dt.strftime('%m月%d日')
|
41 |
-
|
42 |
-
# 删除在“影厅”或“片长”列中缺少数据的行
|
43 |
df.dropna(subset=['影厅', '片长'], inplace=True)
|
44 |
|
45 |
-
# 3.
|
46 |
summary = df.groupby(['日期', '影厅']).agg(
|
47 |
影片数量=('影片', 'count'),
|
48 |
影片播放时长=('片长', 'sum')
|
49 |
).reset_index()
|
50 |
-
|
51 |
-
# 计算广告时长
|
52 |
summary['广告时长'] = summary['影片数量'] * ad_duration
|
53 |
|
54 |
-
# 4.
|
55 |
pivot_table = summary.pivot_table(
|
56 |
index='日期',
|
57 |
columns='影厅',
|
58 |
values=['广告时长', '影片播放时长']
|
59 |
-
)
|
60 |
-
|
61 |
-
# 将所有空白(NaN)值填充为 0
|
62 |
-
pivot_table = pivot_table.fillna(0)
|
63 |
-
|
64 |
-
# 将数值转换为整数,使表格更整洁
|
65 |
-
pivot_table = pivot_table.astype(int)
|
66 |
|
67 |
-
# 交换列的层级顺序并排序,以获得所需的输出格式
|
68 |
if not pivot_table.empty:
|
69 |
pivot_table = pivot_table.swaplevel(0, 1, axis=1).sort_index(axis=1)
|
70 |
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
except Exception as e:
|
75 |
st.error(f"处理文件时出错: {e}")
|
|
|
4 |
|
5 |
st.set_page_config(layout="wide")
|
6 |
|
7 |
+
st.title('影片放映时间表统计')
|
8 |
|
9 |
# 1. 文件上传组件
|
10 |
uploaded_file = st.file_uploader("上传“影片放映时间表.xlsx”文件", type=['xlsx'])
|
11 |
+
ad_duration = st.number_input('输入每个广告的时长(分钟)', min_value=0, value=5)
|
12 |
|
13 |
if uploaded_file is not None:
|
14 |
try:
|
15 |
# 读取Excel文件
|
16 |
df = pd.read_excel(uploaded_file, header=3)
|
17 |
|
|
|
18 |
# 明确将“影片”列转换为字符串类型,以避免混合类型错误
|
19 |
df['影片'] = df['影片'].astype(str)
|
20 |
|
|
|
23 |
|
24 |
|
25 |
# 2. 数据处理和清洗
|
|
|
26 |
def clean_hall_name(name):
|
27 |
if isinstance(name, str):
|
28 |
match = re.search(r'【(\d+)号', name)
|
|
|
32 |
|
33 |
|
34 |
df['影厅'] = df['影厅'].apply(clean_hall_name)
|
|
|
|
|
35 |
df['放映日期'] = pd.to_datetime(df['放映日期'])
|
36 |
df['日期'] = df['放映日期'].dt.strftime('%m月%d日')
|
|
|
|
|
37 |
df.dropna(subset=['影厅', '片长'], inplace=True)
|
38 |
|
39 |
+
# 3. 统计
|
40 |
summary = df.groupby(['日期', '影厅']).agg(
|
41 |
影片数量=('影片', 'count'),
|
42 |
影片播放时长=('片长', 'sum')
|
43 |
).reset_index()
|
|
|
|
|
44 |
summary['广告时长'] = summary['影片数量'] * ad_duration
|
45 |
|
46 |
+
# 4. 创建数据透视表
|
47 |
pivot_table = summary.pivot_table(
|
48 |
index='日期',
|
49 |
columns='影厅',
|
50 |
values=['广告时长', '影片播放时长']
|
51 |
+
).fillna(0).astype(int)
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
|
|
53 |
if not pivot_table.empty:
|
54 |
pivot_table = pivot_table.swaplevel(0, 1, axis=1).sort_index(axis=1)
|
55 |
|
56 |
+
st.subheader('影厅播放统计')
|
57 |
+
|
58 |
+
# --- 表格样式优化 ---
|
59 |
+
# 1. 定义CSS样式
|
60 |
+
styles = [
|
61 |
+
{
|
62 |
+
'selector': 'th.col_heading', # 目标是列标题
|
63 |
+
'props': [
|
64 |
+
('background-color', '#4a4a4a'), # 深色背景
|
65 |
+
('color', 'white'), # 白色字体
|
66 |
+
('text-align', 'center') # 文本居中
|
67 |
+
]
|
68 |
+
},
|
69 |
+
{
|
70 |
+
'selector': 'th.row_heading', # 目标是行标题(日期)
|
71 |
+
'props': [
|
72 |
+
('text-align', 'center')
|
73 |
+
]
|
74 |
+
}
|
75 |
+
]
|
76 |
+
|
77 |
+
# 2. 将样式应用到DataFrame
|
78 |
+
styler = pivot_table.style.set_table_styles(styles)
|
79 |
+
|
80 |
+
# 3. 计算表格的动态高度以实现完全展开
|
81 |
+
# (行数 + 表头层级数 + 额外空间) * 每行高度
|
82 |
+
table_height = (len(pivot_table) + 2 + 1) * 35
|
83 |
+
|
84 |
+
# 4. 使用st.dataframe显示带样式的、完全展开的表格
|
85 |
+
st.dataframe(styler, height=table_height)
|
86 |
+
else:
|
87 |
+
st.warning("没有可用于生成统计信息的数据。")
|
88 |
|
89 |
except Exception as e:
|
90 |
st.error(f"处理文件时出错: {e}")
|