My_first_app / app.py
madsryfeldt's picture
Update app.py
9af0ff8 verified
raw
history blame contribute delete
No virus
1.95 kB
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('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 add filter for room type
selected_room_type = st.sidebar.selectbox('Select Room Type', ['All', 'Private room', 'Entire home/apt', 'Shared room', 'Hotel room'])
# 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)]
if selected_room_type != 'All':
filtered_df = filtered_df[filtered_df['room_type'] == selected_room_type]
# 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.')