미리보기:

앱 주소: http://www.esyoo.site:8502/
앱 설명: https://github.com/eyoo95/Weather_And_Car_Accident
특이사항:
EDA 작업중에 streamlit의 group_by 오류발생
selected_list_for_chart = st.selectbox('차트를 생성하기 원하면 컬럼을 선택하세요',col_list)
if len(selected_list_for_chart) != 0:
if st.button('차트확인'):
st.text('{}년도 {}지역의 {} 변화량을 나타낸 차트입니다.'.format(get_year,get_region,selected_list_for_chart))
fig = plt.figure()
x = df_for_chart.groupby('date')[[selected_list_for_chart]].mean().index
y = df_for_chart.groupby('date')[[selected_list_for_chart]].mean()
plt.xlabel('date')
plt.ylabel(selected_list_for_chart)
plt.xticks(rotation = 45, fontsize=4 )
plt.plot(x,y)
st.pyplot(fig)
# 리스트 두번사용으로 인한 오류
코드가 너무 길어짐
# 전체 지역, 전체 연도
if get_region == '전체' and get_year == '전체':
st.dataframe(df.sort_values(['date','시도별']))
selected_list_for_chart = st.selectbox('차트를 생성하기 원하면 컬럼을 선택하세요',col_list)
if selected_list_for_chart in col_list[4:9]:
if st.button('차트확인'):
st.info('{}년도 {}지역 {}의 합을 나타낸 차트입니다.'.format(get_year,get_region,selected_list_for_chart))
fig = plt.figure()
x = df.groupby('date')[[selected_list_for_chart]].sum().index
y = df.groupby('date')[[selected_list_for_chart]].sum()
plt.xlabel('Date')
plt.ylabel(selected_list_for_chart)
plt.xticks(rotation = 45, fontsize=4 )
plt.plot(x,y)
st.pyplot(fig)
else:
if st.button('차트확인'):
st.info('{}년도 {}지역 {}의 평균변화량을 나타낸 차트입니다.'.format(get_year,get_region,selected_list_for_chart))
fig = plt.figure()
x = df.groupby('date')[[selected_list_for_chart]].mean().index
y = df.groupby('date')[[selected_list_for_chart]].mean()
plt.xlabel('Date')
plt.ylabel(selected_list_for_chart)
plt.xticks(rotation = 45, fontsize=4 )
plt.plot(x,y)
st.pyplot(fig)
# 전체 지역, 특정 연도
elif get_region == region_list[0]:
st.dataframe(df.loc[df['date'].str.contains(get_year),])
selected_list_for_chart = st.selectbox('차트를 생성하기 원하면 컬럼을 선택하세요',col_list)
if selected_list_for_chart in col_list[4:9]:
if st.button('차트확인'):
st.info('{}년도 {}지역 {}의 합을 나타낸 차트입니다.'.format(get_year,get_region,selected_list_for_chart))
fig = plt.figure()
x = df.loc[df['date'].str.contains(get_year),].groupby('date')[[selected_list_for_chart]].sum().index
y = df.loc[df['date'].str.contains(get_year),].groupby('date')[[selected_list_for_chart]].sum()
plt.xlabel('Date')
plt.ylabel(selected_list_for_chart)
plt.xticks(rotation = 45 )
plt.plot(x,y)
st.pyplot(fig)
else:
if st.button('차트확인'):
st.info('{}년도 {}지역 {}의 평균변화량을 나타낸 차트입니다.'.format(get_year,get_region,selected_list_for_chart))
fig = plt.figure()
x = df.loc[df['date'].str.contains(get_year),].groupby('date')[[selected_list_for_chart]].mean().index
y = df.loc[df['date'].str.contains(get_year),].groupby('date')[[selected_list_for_chart]].mean()
plt.xlabel('Date')
plt.ylabel(selected_list_for_chart)
plt.xticks(rotation = 45 )
plt.plot(x,y)
st.pyplot(fig)
# 특정 지역, 전체 연도
elif get_year == year_list[0]:
st.dataframe(df.loc[df['시도별'].str.contains(get_region),])
selected_list_for_chart = st.selectbox('차트를 생성하기 원하면 컬럼을 선택하세요',col_list)
if len(selected_list_for_chart) != 0:
if st.button('차트확인'):
st.info('{}년도 {}지역 {}의 차트입니다.'.format(get_year,get_region,selected_list_for_chart))
fig = plt.figure()
x = df.loc[df['시도별'].str.contains(get_region),]['date']
y = df.loc[df['시도별'].str.contains(get_region),][selected_list_for_chart]
plt.xlabel('Date')
plt.ylabel(selected_list_for_chart)
plt.xticks(rotation = 45, fontsize=4)
plt.plot(x,y)
st.pyplot(fig)
# 특정 지역, 특정 연도
else:
st.dataframe(df.loc[(df['date'].str.contains(get_year))&(df['시도별'].str.contains(get_region)),])
selected_list_for_chart = st.selectbox('차트를 생성하기 원하면 컬럼을 선택하세요',col_list)
if len(selected_list_for_chart) != 0:
if st.button('차트확인'):
st.info('{}년도 {}지역 {}의 차트입니다.'.format(get_year,get_region,selected_list_for_chart))
fig = plt.figure()
x = df.loc[(df['date'].str.contains(get_year))&(df['시도별'].str.contains(get_region)),]['date']
y = df.loc[(df['date'].str.contains(get_year))&(df['시도별'].str.contains(get_region)),][selected_list_for_chart]
plt.xlabel('Date')
plt.ylabel(selected_list_for_chart)
plt.xticks(rotation = 45 )
plt.plot(x,y)
st.pyplot(fig)
'전체'로 if문을 구분하지 않고 join함수를 사용해서 모든 시,도가 나올수 있도록 함
# 전체 행을 나타내기 위한 사전작업
if get_region == region_list[0]:
get_region = '|'.join(region_list[1:])
if get_year == year_list[0]:
get_year = '|'.join(year_list[1:])
region_txt = get_region
year_txt = get_year
if region_txt == '|'.join(region_list[1:]):
region_txt = '전체'
if year_txt == '|'.join(year_list[1:]):
year_txt = '전체'
# 데이터프레임과 차트 나타내기
selected_df = df.loc[(df['date'].str.contains(get_year))&(df['시도별'].str.contains(get_region)),]
st.dataframe(selected_df)
selected_col = st.selectbox('차트를 생성하기 원하면 컬럼을 선택하세요',col_list)
if st.button('차트확인'):
fig = plt.figure()
if selected_col in col_list[4:9]:
st.info('{}년도 {}지역 {}의 합을 나타낸 차트입니다.'.format(year_txt,region_txt,selected_col))
x = selected_df.groupby('date')[[selected_col]].sum().index
y = selected_df.groupby('date')[[selected_col]].sum()
else:
st.info('{}년도 {}지역 {}의 평균변화량을 나타낸 차트입니다.'.format(year_txt,region_txt,selected_col))
x = selected_df.groupby('date')[[selected_col]].mean().index
y = selected_df.groupby('date')[[selected_col]].mean()
plt.xlabel('Date')
plt.ylabel(selected_col)
if year_txt == '전체':
plt.xticks(rotation = 45, fontsize=4 )
else:
plt.xticks(rotation = 45)
plt.plot(x,y)
st.pyplot(fig)
'Streamlit' 카테고리의 다른 글
| [Streamlit] 프로젝트: 생활패턴으로 알아보는 비만정도 예측 앱 (0) | 2022.06.09 |
|---|---|
| [Streamlit] 메뉴 꾸미기 (0) | 2022.05.27 |
| [Streamlit] 로컬에서 만든 앱을 AWS로 배포, 가상환경 구성: joblib (0) | 2022.05.20 |
| [Streamlit] 반응형 차트 만들기 (0) | 2022.05.20 |
| [Streamlit] 차트 그리기 (0) | 2022.05.20 |
댓글