Python

(도전)파이썬 GUI 프로그래밍: streamlit으로 서울 자전거 데이터 분석 구현하기

하방주인장 2023. 5. 11. 12:00
반응형

목차

     

    지난 번에 판다스 없이 진행해보았던 서울 자전거 데이터 분석을 streamlit 이라는 라이브러리를 통해 파이썬 GUI 프로그래밍에 도전해봤습니다. 지난 번에 전처리를 해놓았던 데이터를 csv 파일로 저장하고 불러와서 사용했으니 해당 링크로 들어가 참고하시면 됩니다.

     

    https://openthehabang.tistory.com/12

     

    (도전)파이썬, python: 서울 자전거 데이터 분석을 판다스 없이 해보기

    목차 과제: 월별 자전거 대여량 평균 구하기 * data: SeoulBikeData.csv 출처: https://archive.ics.uci.edu/ml/datasets/Seoul+Bike+Sharing+Demand UCI Machine Learning Repository: Seoul Bike Sharing Demand Data Set Seoul Bike Sharing Demand Dat

    openthehabang.tistory.com

     

    1. streamlit 다운로드

    - 터미널에 입력

    - 가상환경을 만들어서 설치하는 것이 좋음

    pip install streamlit

    https://streamlit.io/

     

    Streamlit • A faster way to build and share data apps

    Streamlit is an open-source app framework for Machine Learning and Data Science teams. Create beautiful web apps in minutes.

    streamlit.io

     

    2. code

    import streamlit as st
    import pandas as pd
    
    st.markdown(
        """
    <style>
    span[data-baseweb="tag"] {
      background-color: blue !important;
    }
    </style>
    """,
        unsafe_allow_html=True,
    )
    
    
    st.title('Seoul Bike :bike:')
    
    df = pd.read_csv('../0_data/SeoulBikeData_added.csv')
    
    columns = df.columns
    group_by_option = st.selectbox('Group By: ', ['None'] + list(columns))
    cols = st.multiselect('컬럼 선택:', list(columns))
    
    if group_by_option == 'None':
        if cols:
            tmp_df = df[cols] # 특정 컬럼 선택 cols = ['col1', 'col2']
        else:
            tmp_df = df
    
    else: #groubby를 할 컬럼이 선택된 경우
        if cols:
            tmp_df = df.groupby(group_by_option).mean()[cols]
        else:
            tmp_df = df.groupby(group_by_option).mean()
    
    
    df_tab, plot_tab = st.tabs(['테이블', '시각화'])
    with df_tab:
        st.dataframe(tmp_df)
    
    with plot_tab:
        st.line_chart(tmp_df)

     

    3. 실행 및 결과

    - 터미널에서 streamlit run 파일이름.py 로 실행