krisnadwipaj's picture
update app.py
65a6759
import streamlit as st
import pandas as pd
import plotly.express as px
import math
complete_acc = pd.read_csv('completedacct.csv')
complete_district = pd.read_csv('completeddistrict.csv')
complete_loan = pd.read_csv('completedloan.csv')
#clean data
complete_acc = complete_acc.drop(['year', 'month', 'day', 'date'], axis = 1)
#set up the data
acc_loan = complete_loan.merge(complete_acc, how='inner', left_on='account_id', right_on='account_id')
acc_loan_district = acc_loan.merge(complete_district, how='inner', left_on='district_id', right_on='district_id')
year = acc_loan_district['year'].isin([2013, 2014])
acc_loan_district = acc_loan_district[year]
total_loan = acc_loan_district['amount'].sum()
growth_rate = acc_loan_district.groupby('year')['amount'].sum().reset_index()
growth_rate['lead(1)'] = growth_rate['amount'].shift(1)
growth_rate['growth_rate'] = ((growth_rate['amount'] - growth_rate['lead(1)'])/growth_rate['lead(1)'])*100
growth_rate_fix = math.ceil(growth_rate['growth_rate'].iloc[1])
loan_per_city = acc_loan_district.groupby('city')['amount'].sum().reset_index().sort_values('amount', ascending = False)
loan_per_city = loan_per_city.head(10)
loan_per_region = acc_loan_district.groupby('region')['amount'].sum().reset_index().sort_values('amount', ascending = False)
region = acc_loan_district['region'].unique().tolist()
region.append('All')
def get_filtered_data(df, region):
if region == 'All':
mask_region = df['region'].isin(['Northeast', 'South', 'Midwest', 'West'])
else:
mask_region = df['region'] == region
return df[mask_region]
def line_plot(df):
df = df.groupby('month')['amount'].sum().reset_index()
fig = px.line(df, x = df['month'], y = df['amount'], title = 'Trend Loan per Month')
return fig
def bar_plot(df, x, y, title):
fig = px.bar(df, x=x, y=y, title=title)
return fig
st.title('Total Loans Period 2013-2014')
col1, col2 = st.columns(2)
col1.metric(label='Total', value='$'+str(total_loan))
col2.metric(label='Growth Rate in 12 Month', value='%'+str(growth_rate_fix))
st.plotly_chart(bar_plot(loan_per_city, loan_per_city['city'], loan_per_city['amount'], 'By City'))
st.plotly_chart(bar_plot(loan_per_region, loan_per_region['region'], loan_per_region['amount'], 'By Region'))
st.markdown('Trend Filter')
selected_region = st.selectbox('Select Region', region, key = 'region')
df_filtered = get_filtered_data(acc_loan_district, selected_region)
st.plotly_chart(line_plot(df_filtered))