Spaces:
Sleeping
Sleeping
| #Import my packages | |
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import altair as alt | |
| from scipy.stats import zscore | |
| #Giving my app a name | |
| st.title('Assignment') | |
| #Importing my dataset | |
| file_path = 'loan_themes_by_region.csv' | |
| df = pd.read_csv(file_path) | |
| #Cleaning data | |
| df.drop(['Partner ID','Loan Theme ID','forkiva','geocode_old','ISO', 'geocode','names','geo','mpi_region','mpi_geo','rural_pct'], axis="columns", inplace=True) | |
| z_scores = zscore(df['amount']) | |
| df['outlier_amount'] = (z_scores > 3) | (z_scores < -3) | |
| df = df[~df['outlier_amount']] | |
| #Doing a text "box" about Kiva | |
| st.markdown(""" | |
| Welcome to the Kiva Loan Themes By Region Dashboard.""") | |
| st.markdown("Kiva is a global crowdfunding platform dedicated to expanding financial access for underserved communities around the world. ") | |
| st.markdown('By connecting lenders with individuals and small businesses in need of financial support, Kiva enables them to achieve their goals and improve their livelihoods. ') | |
| st.markdown('Since its inception, Kiva has facilitated over $1 billion in loans to more than 2 million people, empowering entrepreneurs and fostering economic growth in regions where traditional financial services are limited or unavailable. ') | |
| st.markdown('Through a collaborative approach, Kiva aims to create a more inclusive and equitable financial system for all.') | |
| # Tutorial Expander | |
| with st.expander("**How to Use the Dashboard 📚**"): | |
| st.markdown(""" | |
| 1. **Filter Data** - Use the sidebar filters to narrow down specific data sets. | |
| 2. **Visualize Data** - From the dropdown, select a visualization type to view patterns. | |
| 3. **Insights & Recommendations** - Scroll down to see insights derived from the visualizations and actionable recommendations. | |
| """) | |
| # Doing an expander for Objectives | |
| with st.expander("**Objectives**"): | |
| st.markdown(""" | |
| **The main objectives of this Kiva Loan Themes By Region Dashboard are:** | |
| 1. **Understand Kiva’s Lending Activities:** This dashboard provides a clear overview of how Kiva's loans are distributed across different countries and regions, helping you see where the support is going. | |
| 2. **Analyze Loan Sizes:** By showing key statistics like the average, median, and total loan amounts, you can get a sense of how much is typically lent in the selected country. | |
| 3. **Filter by Country and Partners:** Easily filter the data to focus on specific countries or field partners, allowing you to dive deeper into their loan activities. | |
| 4. **Remove Extreme Values:** We’ve filtered out unusually large or small loan amounts to ensure the data is reliable and easier to interpret, giving you a clearer view of the main trends. | |
| 5. **Interactive Data Exploration:** The dashboard includes interactive charts and visualizations, so you can explore different aspects of the data in a dynamic way. | |
| 6. **Gain Insights for Decision Making:** The combination of filters, statistics, and visuals helps you make informed insights about how Kiva loans are impacting different sectors and regions. | |
| """) | |
| #Making a selectbar for countries | |
| selected_country = st.sidebar.selectbox("Select Country", df['country'].unique().tolist()) | |
| if not selected_country: | |
| st.warning("Please select a Country from the sidebar ⚠️") | |
| st.stop() | |
| filtered_df = df[df['country'] == (selected_country)] | |
| # Introduce the table with a clear explanation | |
| st.markdown(""" | |
| ### Data Table for Selected Country | |
| This table shows detailed loan information for the country you select from the sidebar filter. | |
| **How to Use the Filter:** | |
| 1. **Select a Country:** Use the dropdown menu in the sidebar on the left side of the screen to choose a country. | |
| 2. **View the Data:** Once a country is selected, the table will automatically update to display only the loan data for that country. | |
| If no country is selected, the table will not display any data. Make sure to select a country to view relevant information. | |
| **Column Descriptions:** | |
| - **Loan Amount:** The total amount of each loan provided. | |
| - **Field Partner Name:** The organization or individual facilitating the loan. | |
| - **Loan Theme Type:** The category or sector to which the loan is allocated. | |
| - **Other Relevant Information:** Additional details related to the loans. | |
| Use this table to explore and analyze the loan details specific to the chosen country. | |
| """) | |
| #Making a table | |
| st.dataframe(filtered_df) | |
| # Display a brief explanation of the statistics | |
| st.markdown(""" | |
| ### Key Statistics for Selected Country | |
| The following statistics provide insights into the loan amounts for the selected country. These metrics help you understand the typical loan size and overall distribution. | |
| - **Mean:** The average loan amount. | |
| - **Mode:** The most frequently occurring loan amount. | |
| - **Median:** The middle loan amount when sorted. | |
| - **Sum:** The total of all loan amounts. | |
| """) | |
| # Adding an expander for detailed statistical terms | |
| with st.expander("**Statistical Terms Explained 📘**"): | |
| st.markdown(""" | |
| **Mean:** The mean is the average of a list of numbers. Add all the numbers together and then divide by the count of numbers. For example, for 4, 6, and 8, the mean is (4 + 6 + 8) / 3 = 6. | |
| **Mode:** The mode is the number that appears most frequently in a list. For instance, in 2, 4, 4, 6, 7, the mode is 4, as it appears more than any other number. | |
| **Median:** The median is the middle value in a list arranged from smallest to largest. If there’s an even number of values, it’s the average of the two middle numbers. For example, in 3, 5, 7, the median is 5. In 1, 3, 5, 7, the median is (3 + 5) / 2 = 4. | |
| **Sum:** The sum is the total of all numbers added together. For example, the sum of 2, 3, and 5 is 10 (2 + 3 + 5). | |
| """) | |
| # If the filtered DataFrame is not empty, calculate statistics | |
| if not filtered_df.empty: | |
| # Calculate mode, mean, and median for the 'amount' column in the filtered data | |
| mode_value = round(filtered_df['amount'].mode()[0],1) | |
| mean_value = round(filtered_df['amount'].mean(),1) | |
| median_value = round(filtered_df['amount'].median(),1) | |
| sum_value = round(filtered_df['amount'].sum(),1) | |
| # Display these statistics using columns | |
| col1, col2, col3, col4 = st.columns(4) | |
| col1.metric(label=f"Mode of Amount for {selected_country}", value=mode_value) | |
| col2.metric(label=f"Mean of Amount for {selected_country}", value=mean_value) | |
| col3.metric(label=f"Median of Amount for {selected_country}", value=median_value) | |
| col4.metric(label=f"Sum of Amount for {selected_country}", value=sum_value) | |
| else: | |
| st.warning(f"No data available for {selected_country}") | |
| # Making an Altair chart for Field Partner Sum Amount | |
| st.markdown(""" | |
| ### Field Partner Loan Amounts | |
| This bar chart displays the total loan amount provided by each field partner in the selected country. | |
| **What You See:** | |
| - **Y-Axis:** Total loan amount provided by each field partner. | |
| - **X-Axis:** Field partner names, sorted by the total loan amount in descending order. | |
| - **Bar Height:** Represents the total loan amount associated with each field partner. | |
| Use this chart to compare the total loan amounts across different field partners and identify which partners are handling the largest loan volumes. | |
| """) | |
| chart = alt.Chart(filtered_df).mark_bar().encode( | |
| y='sum(amount)', | |
| x=alt.X('Field Partner Name', sort='-y', axis=alt.Axis(labelAngle=-45, labelOverlap=False, labelFontSize=10)) | |
| ).properties( | |
| title='Field Partner Sum Amount' | |
| ) | |
| st.altair_chart(chart, use_container_width=True) | |
| # Creating a pie chart with Altair | |
| st.markdown(""" | |
| ### Loan Amount Distribution by Sector | |
| This pie chart shows the distribution of loan amounts by sector for the selected field partner. | |
| **What You See:** | |
| - **Slices:** Each slice represents the proportion of the total loan amount allocated to different loan themes. | |
| - **Color:** Represents different loan themes. | |
| - **Size:** Reflects the amount of loans distributed across these themes. | |
| Use this chart to understand the sectoral distribution of loans managed by the selected field partner. | |
| """) | |
| field_partner_options = filtered_df['Field Partner Name'].unique() | |
| selected_partner = st.selectbox('Select a Field Partner', field_partner_options) | |
| pie_chart_data = filtered_df[filtered_df['Field Partner Name'] == selected_partner].groupby('Loan Theme Type')['amount'].sum().reset_index() | |
| pie_chart_data.columns = ['Loan Theme Type', 'amount'] | |
| chart = alt.Chart(pie_chart_data).mark_arc().encode( | |
| theta=alt.Theta(field='amount', type='quantitative'), | |
| color=alt.Color(field='Loan Theme Type', type='nominal') | |
| ).properties( | |
| title=f'Loan Amount Distribution by Sector for {selected_partner}', | |
| width=300, | |
| height=300 | |
| ) | |
| st.altair_chart(chart, use_container_width=True) | |