File size: 4,092 Bytes
addfe87
d08ab74
 
 
 
 
fc584b7
d08ab74
 
 
bc34780
d08ab74
6694af2
 
921748b
 
 
 
 
 
6694af2
 
 
 
d4a09ac
2485e4d
9ff09ee
d08ab74
 
40e2ac9
6694af2
 
 
d08ab74
 
 
 
 
bc34780
 
 
d08ab74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d23d2fe
 
 
 
 
 
 
768cc19
d08ab74
 
 
 
 
 
 
 
6694af2
 
d08ab74
9ff09ee
 
d08ab74
d4a09ac
921748b
6694af2
 
d08ab74
 
3d5e60a
d08ab74
80ed3a0
bd20aea
 
 
 
 
 
 
80ed3a0
bd20aea
80ed3a0
 
 
bd20aea
80ed3a0
9db69bc
bd3e270
b6e588c
847df83
bd3e270
921748b
6694af2
 
5d59349
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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:
        # NanumGothic-Regular.ttf 폰트 경둜 μ„€μ •
        font_path = 'NanumGothic-Regular.ttf'
        fontprop = font_manager.FontProperties(fname=font_path)
        
        # Matplotlib에 폰트 μ„€μ •
        plt.rc('font', family=fontprop.get_name())
        #plt.rc('font', family='NanumGothicCoding')

        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)
    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("이미지 νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")