My_first_app / app.py
madsryfeldt's picture
Update app.py
3eef162 verified
raw
history blame
No virus
1.93 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('AirBnB bookings in Copenhagen')
# 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', ['Private room', 'Entire home/apt'])
# 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:
filtered_df = filtered_df[filtered_df['room_type'] == selected_room_type]
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
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 histogram for each neighbourhood
if not avg_prices_pivot.empty:
st.write('Histogram of Average Price for Each Neighbourhood:')
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.')