Welcome to New York City, one of the most-visited cities in the world. There are many Airbnb listings in New York City to meet the high demand for temporary lodging for travelers, which can be anywhere between a few nights to many months. In this project, we will take a closer look at the New York Airbnb market by combining data from multiple file types like .csv
, .tsv
, and .xlsx
.
Recall that CSV, TSV, and Excel files are three common formats for storing data. Three files containing data on 2019 Airbnb listings are available to you:
data/airbnb_price.csv This is a CSV file containing data on Airbnb listing prices and locations.
listing_id
: unique identifier of listingprice
: nightly listing price in USDnbhood_full
: name of borough and neighborhood where listing is located
data/airbnb_room_type.xlsx This is an Excel file containing data on Airbnb listing descriptions and room types.
listing_id
: unique identifier of listingdescription
: listing descriptionroom_type
: Airbnb has three types of rooms: shared rooms, private rooms, and entire homes/apartments
data/airbnb_last_review.tsv This is a TSV file containing data on Airbnb host names and review dates.
listing_id
: unique identifier of listinghost_name
: name of listing hostlast_review
: date when the listing was last reviewed
# Import necessary packages
import pandas as pd
import numpy as np
# Begin coding here ...
# Use as many cells as you like
#load data from the data folder
#load the csv data using read_csv function from pandas
from pandas import read_csv
a_prices= read_csv('data/airbnb_price.csv')
print(a_prices)
#load the xls file
a_room_type= 'data/airbnb_room_type.xlsx'
rtype= pd.ExcelFile(a_room_type)
print(rtype.sheet_names)
room_t= pd.read_excel(rtype)
room_t.head()
#load the tsv file using the read_csv
from pandas import read_csv
review= read_csv('data/airbnb_last_review.tsv', sep= '\t')
print(review)
#Merging the dataframes using their unique identifiers listing_id
price_type= a_prices.merge(room_t,how= 'right', on= 'listing_id' )
df_merged= price_type.merge(review,how='left', on='listing_id')
print(df_merged.head())
column_names= df_merged.columns
print(column_names)
df_merged
#determine the earliest and the most recent review dates
#convert relevant review column to date format using pd.to_datetime
df_dated= pd.to_datetime(df_merged['last_review'], infer_datetime_format=True, errors= "coerce")
print(df_dated)
#find the earliest and the most recent reviews
earliest_review=df_dated.iloc[df_dated. argmin()]
most_recent_review=df_dated.max()
print(earliest_review)
print(most_recent_review)
#sort the data by data_review dates
review_sorted_dates= df_dated.sort_values()
print(review_sorted_dates)
#sum of private rooms
#print the room_type column
print(df_merged['room_type'])
#find unique values of the room_type attributes
df_merged['room_type'].unique()
#convert from object to string data type
df_merged['room_type']= df_merged['room_type'].astype('string')
df_merged['room_type'].info()
#unique values
df_merged['room_type'].unique()
#standardizing capitalization in string data to all lower case
df_merged['room_type']= df_merged['room_type'].str.lower()
print(df_merged['room_type'].unique())
# counting the number of private rooms
import pandas as pd
# Assuming df_merged is already defined somewhere in the notebook
airbnb_df = pd.DataFrame(df_merged)
private_rooms = airbnb_df.loc[airbnb_df['room_type'] == 'private room']
nb_p_rooms = private_rooms.shape[0]
print(nb_p_rooms)
# 11356 private rooms
# To find the average price of listings
# Get the price column
airbnb_df['price']
# Ensure the 'price' column is of string type
airbnb_df['price'] = airbnb_df['price'].astype(str)
# Replace 'dollars' with an empty space
airbnb_df['price'] = airbnb_df['price'].str.replace('dollars', '')
# Remove any extra spaces and convert to float
airbnb_df['price'] = airbnb_df['price'].str.strip().astype(float)
print(airbnb_df['price'])
#find mean
mean_price=(airbnb_df['price'].mean().round(2))
print(mean_price)
#combine new variables
#loading solutions into a dataframe
import pandas as pd
review_dates = pd.DataFrame({'first_reviewed':[earliest_review],
'last_reviewed':[most_recent_review],
'nb_private_rooms':[nb_p_rooms],
'avg_price':[mean_price]})
display(review_dates)