Skip to content

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 listing
  • price: nightly listing price in USD
  • nbhood_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 listing
  • description: listing description
  • room_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 listing
  • host_name: name of listing host
  • last_review: date when the listing was last reviewed
import pandas as pd

# Load the data from each file
price_df = pd.read_csv('data/airbnb_price.csv')
room_type_df = pd.read_excel('data/airbnb_room_type.xlsx')
last_review_df = pd.read_csv('data/airbnb_last_review.tsv', delimiter='\t')

# Clean 'room_type' data
room_type_df['room_type'] = room_type_df['room_type'].str.lower()

# Merge DataFrames on 'listing_id'
merged_df = pd.merge(price_df, room_type_df, on='listing_id')
merged_df = pd.merge(merged_df, last_review_df, on='listing_id')

# Convert 'last_review' to datetime format
merged_df['last_review'] = pd.to_datetime(merged_df['last_review'])

# Find the earliest and most recent review dates
earliest_review = merged_df['last_review'].min()
most_recent_review = merged_df['last_review'].max()

# Count the number of private room listings
private_rooms_count = merged_df[merged_df['room_type'] == 'private room'].shape[0]

# Convert 'price' to numeric and calculate the average listing price
merged_df['price'] = merged_df['price'].str.replace(' dollars', '').astype(float)
average_price = merged_df['price'].mean()

# Combine variables into a DataFrame
review_dates = pd.DataFrame({
    'first_reviewed': [earliest_review],
    'last_reviewed': [most_recent_review],
    'nb_private_rooms': [private_rooms_count],
    'avg_price': [round(average_price, 2)]
})

print(review_dates)

A brief explanation of what each line of code above does.

import pandas as pd # Load the data from each file price_df = pd.read_csv('data/airbnb_price.csv') # Reads a CSV file containing price data into a DataFrame room_type_df = pd.read_excel('data/airbnb_room_type.xlsx') # Reads an Excel file containing room type data into a DataFrame last_review_df = pd.read_csv('data/airbnb_last_review.tsv', delimiter='\t') # Reads a TSV file containing review dates into a DataFrame # Clean 'room_type' column room_type_df['room_type'] = room_type_df['room_type'].str.lower() # Converts the 'room_type' column to lowercase # Merge DataFrames on 'listing_id' merged_df = pd.merge(price_df, room_type_df, on='listing_id') # Merges 'price_df' and 'room_type_df' on 'listing_id' merged_df = pd.merge(merged_df, last_review_df, on='listing_id') # Further merges the result with 'last_review_df' on 'listing_id' # Convert 'last_review' to datetime format merged_df['last_review'] = pd.to_datetime(merged_df['last_review'], format='%Y-%m-%d') # Converts 'last_review' column to datetime format # Find the earliest and most recent review dates earliest_review = merged_df['last_review'].min() # Finds the earliest review date most_recent_review = merged_df['last_review'].max() # Finds the most recent review date # Count the number of private room listings private_rooms_count = merged_df[merged_df['room_type'] == 'private room'].shape[0] # Counts the number of listings with 'private room' in 'room_type' # Convert 'price' to numeric and calculate the average listing price merged_df['price'] = merged_df['price'].replace('[\$,]', '', regex=True).astype(float) # Removes dollar signs and commas, then converts 'price' to float average_price = merged_df['price'].mean() # Calculates the average price # Combine variables into a DataFrame review_dates = pd.DataFrame({ 'first_reviewed': [earliest_review], # Creates a DataFrame with the earliest review date 'last_reviewed': [most_recent_review], # Creates a DataFrame with the most recent review date 'nb_private_rooms': [private_rooms_count], # Creates a DataFrame with the count of private room listings 'avg_price': [round(average_price, 2)] # Creates a DataFrame with the average price rounded to 2 decimal places }) print(review_dates) # Prints the resulting DataFrame