book_pattern / app.py
dltmdgus's picture
Update app.py
65bfa8f verified
raw
history blame
3.75 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.font_manager as fm
from PIL import Image
# 폰트 μ„€μ •
font_path = r"H2GTRM.TTF"
font_prop = fm.FontProperties(fname=font_path, size=12)
plt.rcParams['font.family'] = font_prop.get_name()
plt.rcParams['axes.unicode_minus'] = False
# μ‚¬μ΄λ“œλ°”μ— λͺ©μ°¨ μΆ”κ°€
st.sidebar.title("λͺ©μ°¨")
page = st.sidebar.radio("νŽ˜μ΄μ§€ 선택", ["λͺ¨λ“  λŒ€μΆœ λ„μ„œ μƒμœ„ 10개", "μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 5개 λŒ€μΆœ λ„μ„œ", "μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 3개 μž₯λ₯΄"])
if page == "λͺ¨λ“  λŒ€μΆœ λ„μ„œ μƒμœ„ 10개":
st.title("λͺ¨λ“  λŒ€μΆœ λ„μ„œ μƒμœ„ 10개")
# 이미지 파일 경둜
image_path = r'top_books_plot_page-0001.jpg'
# 이미지 μ—…λ‘œλ“œ 및 ν‘œμ‹œ
st.image(image_path, caption="λͺ¨λ“  λŒ€μΆœ λ„μ„œ μƒμœ„ 10개", use_column_width=True)
elif page == "μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 5개 λŒ€μΆœ λ„μ„œ":
st.title("μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 5개 λŒ€μΆœ λ„μ„œ")
# 데이터 λ‘œλ“œ
file_path = r'μƒμœ„_5_λ„μ„œ.xlsx'
df = pd.read_excel(file_path, sheet_name=None)
df = pd.concat(df.values()) # λͺ¨λ“  μ‹œνŠΈ 데이터λ₯Ό κ²°ν•©
# μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ λ„μ„œ λŒ€μΆœ 건수 집계
book_patterns = df.groupby(['λ„μ„œλͺ…', 'μ—°λ Ή', '성별'])['λŒ€μΆœκ±΄μˆ˜'].sum().reset_index()
# μƒμœ„ 5개 λ„μ„œ μΆ”μΆœ ν•¨μˆ˜
def get_top_books(data, top_n=5):
return data.groupby(['μ—°λ Ή', '성별']).apply(lambda x: x.nlargest(top_n, 'λŒ€μΆœκ±΄μˆ˜')).reset_index(drop=True)
# μƒμœ„ 5개의 λ„μ„œ 데이터 μΆ”μΆœ
top_books = get_top_books(book_patterns)
# μ—°λ ΉλŒ€μ™€ 성별 선택
ages = top_books['μ—°λ Ή'].unique()
genders = top_books['성별'].unique()
selected_age = st.selectbox("μ—°λ ΉλŒ€ 선택", options=ages)
selected_gender = st.selectbox("성별 선택", options=genders)
filtered_books = top_books[(top_books['μ—°λ Ή'] == selected_age) & (top_books['성별'] == selected_gender)]
# λ§‰λŒ€ κ·Έλž˜ν”„ μ‹œκ°ν™”
if not filtered_books.empty:
st.write(f"μƒμœ„ 5 λ„μ„œ (μ—°λ ΉλŒ€: {selected_age}, 성별: {selected_gender})")
fig, ax = plt.subplots(figsize=(10, 6))
sns.barplot(data=filtered_books, x='λ„μ„œλͺ…', y='λŒ€μΆœκ±΄μˆ˜', palette='viridis', ax=ax)
ax.set_title(f'{selected_age} - {selected_gender}의 μƒμœ„ 5 λ„μ„œ')
ax.set_xlabel('λ„μ„œλͺ…')
ax.set_ylabel('λŒ€μΆœκ±΄μˆ˜')
plt.xticks(rotation=45, ha='right')
st.pyplot(fig)
elif page == "μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 3개 μž₯λ₯΄":
st.title("μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 3개 μž₯λ₯΄")
# 데이터 λ‘œλ“œ
file_path = r'μƒμœ„_3_μž₯λ₯΄.xlsx'
df = pd.read_excel(file_path, sheet_name=None)
df = pd.concat(df.values()) # λͺ¨λ“  μ‹œνŠΈ 데이터λ₯Ό κ²°ν•©
# μ—°λ ΉλŒ€ λ¬Έμžμ—΄ 처리
df['μ—°λ ΉλŒ€'] = df['μ—°λ Ή'].astype(str)
# μž₯λ₯΄λ³„ λŒ€μΆœ 건수 집계
genre_age_sex = df.groupby(['μ—°λ ΉλŒ€', '성별', 'μ£Όμ œλΆ„λ₯˜λͺ…'])['λŒ€μΆœκ±΄μˆ˜'].sum().unstack().fillna(0)
# 히트맡 μ‹œκ°ν™”
st.write("μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μž₯λ₯΄ μ„ ν˜Έλ„ 히트맡")
fig, ax = plt.subplots(figsize=(12, 8))
sns.heatmap(genre_age_sex, annot=False, cmap='YlGnBu', linewidths=0.5, ax=ax)
ax.set_title('μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μž₯λ₯΄ μ„ ν˜Έλ„')
st.pyplot(fig)
# 히트맡 이미지 파일 경둜
heat_map_image_path = r'heat_map_page-0001.jpg'
# 이미지 μ—…λ‘œλ“œ 및 ν‘œμ‹œ
st.image(heat_map_image_path, caption="μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μž₯λ₯΄ μ„ ν˜Έλ„ 히트맡", use_column_width=True)