Spaces:
Runtime error
Runtime error
import pandas as pd | |
import streamlit as st | |
import matplotlib.pyplot as plt | |
# Read the CSV file into a Pandas DataFrame | |
df = pd.read_csv('/Users/mads/Desktop/Cand. Merc IM/2. semester/2.4 Digital Innovation/1. Introduction/S1_files/cph_airbnb_listings.csv') | |
# Create a Streamlit app | |
st.title('Sorted and Filtered Data by Neighbourhood and Price') | |
# Allow user to add filters for neighbourhoods | |
selected_neighbourhoods = st.sidebar.multiselect('Select Neighbourhood(s)', df['neighbourhood'].unique()) | |
# Allow user to set a price range filter | |
price_range = st.sidebar.slider('Select Price Range', min_value=0, max_value=1000, step=10, value=(0, 1000)) | |
# Filter the DataFrame based on selected filters | |
filtered_df = df.copy() | |
if selected_neighbourhoods: | |
filtered_df = filtered_df[filtered_df['neighbourhood'].isin(selected_neighbourhoods)] | |
filtered_df = filtered_df[(filtered_df['price'] >= price_range[0]) & (filtered_df['price'] <= price_range[1])] | |
# Display the filtered DataFrame | |
st.write('Below is the sorted and filtered data:') | |
st.write(filtered_df) | |
# Calculate the average price for each selected neighbourhood and room type | |
avg_prices = filtered_df.groupby(['neighbourhood', 'room_type'])['price'].mean().reset_index() | |
# Pivot the DataFrame to have neighbourhoods as index and room types as columns | |
avg_prices_pivot = avg_prices.pivot(index='neighbourhood', columns='room_type', values='price') | |
# Plot a bar chart for average price of both room types in each neighbourhood | |
if not avg_prices_pivot.empty: | |
st.write('Bar Chart of Average Price for Each Neighbourhood and Room Type:') | |
fig, ax = plt.subplots() | |
avg_prices_pivot.plot(kind='bar', ax=ax) | |
ax.set_xlabel('Neighbourhood') | |
ax.set_ylabel('Average Price') | |
plt.xticks(rotation=45, ha='right') | |
st.pyplot(fig) | |
else: | |
st.write('No data available for selected filters.') | |