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