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
file = 'data/airbnb_last_review.tsv'# What are the dates of the earliest and most recent reviews? Store these values as two separate variables with your preferred names.
import pandas as pd
reviews = pd.read_csv(file, sep='\t')
reviews['last_review'] = pd.to_datetime(reviews['last_review'], format='%B %d %Y')
reviews.head()
first_reviewed = reviews['last_review'].min()
last_reviewed = reviews['last_review'].max()
first_reviewed, last_reviewedprint(first_reviewed, last_reviewed)# How many of the listings are private rooms? Save this into any variable.
import pandas as pd
file1 = 'data/airbnb_room_type.xlsx'
room_type = pd.read_excel(file1)
types_rooms = room_type['room_type'].value_counts()
#types_rooms shows multiple ways "private room" appears so regularizing that entry to all lower case
clean_up_room_type = room_type['room_type'].str.lower()
private_rooms = clean_up_room_type[clean_up_room_type == 'private room']
nb_private_rooms = private_rooms.shape[0]
print(nb_private_rooms)# What is the average listing price? Round to the nearest two decimal places and save into a variable.
import pandas as pd
file2 = 'data/airbnb_price.csv'
listings = pd.read_csv(file2, sep=',')
# Assuming the column with prices is named 'price' and contains strings with 'dollars'
listings['price'] = listings['price'].str.replace('dollars', '').astype(float)
# Calculate the average price
avg_price = round(listings['price'].mean(), 2)
avg_price# Combine the new variables into one DataFrame called review_dates with four columns in the following order:
# first_reviewed, last_reviewed, nb_private_rooms, and avg_price. The DataFrame should only contain one row of values.
review_dates = pd.DataFrame([[first_reviewed, last_reviewed, nb_private_rooms, avg_price]],
columns=['first_reviewed', 'last_reviewed', 'nb_private_rooms', 'avg_price'])
review_dates.head()