book_pattern / app.py
dltmdgus's picture
Update app.py
48d8b46 verified
Raw
History Blame Contribute Delete
4.09 kB
import os
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.font_manager as fm
from matplotlib import rc
# μ‚¬μ΄λ“œλ°”μ— λͺ©μ°¨ μΆ”κ°€
st.sidebar.title("λͺ©μ°¨")
page = st.sidebar.radio("νŽ˜μ΄μ§€ 선택", ["μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 5개 λŒ€μΆœ λ„μ„œ", "μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 3개 μž₯λ₯΄"])
def load_data(file_path):
try:
if os.path.isfile(file_path):
df = pd.read_excel(file_path, sheet_name=None)
return pd.concat(df.values())
else:
st.error(f"νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€: {file_path}")
return pd.DataFrame()
except Exception as e:
st.error(f"데이터λ₯Ό λ‘œλ“œν•˜λŠ” λ™μ•ˆ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
return pd.DataFrame()
if page == "μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 5개 λŒ€μΆœ λ„μ„œ":
st.title("μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 5개 λŒ€μΆœ λ„μ„œ")
# 데이터 λ‘œλ“œ
df = load_data('μƒμœ„_5_λ„μ„œ.xlsx')
if df.empty:
st.stop()
# μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ λ„μ„œ λŒ€μΆœ 건수 집계
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:
font_path = 'NanumGothic-Regular.ttf'
fontprop = fm.FontProperties(fname=font_path)
plt.rc('font', family=fontprop.get_name())
plt.rcParams['font.family'] = fontprop.get_name()
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 λ„μ„œ', fontproperties=fontprop)
ax.set_xlabel('λ„μ„œλͺ…', fontproperties=fontprop)
ax.set_ylabel('λŒ€μΆœκ±΄μˆ˜', fontproperties=fontprop)
plt.xticks(rotation=45, ha='right', fontproperties=fontprop)
st.pyplot(fig)
else:
st.write("μ„ νƒν•œ 쑰건에 λ§žλŠ” 데이터가 μ—†μŠ΅λ‹ˆλ‹€.")
elif page == "μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 3개 μž₯λ₯΄":
st.title("μ—°λ ΉλŒ€ 및 성별에 λ”°λ₯Έ μƒμœ„ 3개 μž₯λ₯΄")
# 데이터 λ‘œλ“œ
df = load_data('μƒμœ„_3_μž₯λ₯΄.xlsx')
if df.empty:
st.stop()
# μ—°λ ΉλŒ€ λ¬Έμžμ—΄ 처리
df['μ—°λ ΉλŒ€'] = df['μ—°λ ΉλŒ€'].astype(str)
# μ—°λ ΉλŒ€ 및 성별 선택
ages = df['μ—°λ ΉλŒ€'].unique()
genders = df['성별'].unique()
selected_age = st.selectbox("μ—°λ ΉλŒ€ 선택", options=ages)
selected_gender = st.selectbox("성별 선택", options=genders)
# ν•„ν„°λ§λœ 데이터 μΆ”μΆœ
filtered_df = df[(df['μ—°λ ΉλŒ€'] == selected_age) & (df['성별'] == selected_gender)]
# 주제 λΆ„λ₯˜λͺ… λͺ©λ‘ 좜λ ₯
genre_list = filtered_df['μ£Όμ œλΆ„λ₯˜λͺ…'].unique()
st.write(f"{selected_age} - {selected_gender}에 λ”°λ₯Έ 주제 λΆ„λ₯˜λͺ…:")
st.write(genre_list)
# 이미지 파일 경둜
image_path = 'heat_map_page-0001.jpg'
# 이미지 μ—…λ‘œλ“œ 및 ν‘œμ‹œ
if os.path.isfile(image_path):
st.image(image_path, caption="히트맡", use_column_width=True)
else:
st.write("이미지 νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")