Booked revenue by city

dates

Booked revenue by city

Airbnb Pandas Interview Question

Airbnb's city teams report how many nights were booked and how much those stays earned.

Using the bookings and listings DataFrames, and completed bookings only, return each city with the total nights booked as nights and the revenue as revenue. A stay's nights are the days between check-in and check-out, and its revenue is nights times the listing's nightly price. Sort the rows by revenue, highest first. Assign the answer to result.

Asked of

  • Data Analyst
  • Product Analyst
  • Business Analyst
  • Analytics Engineer
  • Data Scientist

bookingsDataFrame16 rows

Column NameType
booking_idint64
listing_idint64
guest_idint64
check_instr
check_outstr
statusstr

listingsDataFrame10 rows

Column NameType
listing_idint64
host_idint64
citystr
room_typestr
price_per_nightint64

bookingsExample Input

booking_idlisting_idguest_idcheck_incheck_outstatus
50011012012024-06-012024-06-05completed
50021032022024-06-022024-06-04completed
50031062032024-06-032024-06-10completed
50041022042024-06-052024-06-06canceled
50051042052024-06-072024-06-09completed
50061092062024-06-082024-06-11canceled
50071102072024-06-102024-06-15completed
50081052082024-06-122024-06-13completed

listingsExample Input

listing_idhost_idcityroom_typeprice_per_night
10111LisbonEntire home120
10211LisbonPrivate room55
10312ParisEntire home210
10413ParisPrivate room85
10513ParisShared room40
10614TokyoEntire home160
10715TokyoPrivate room70
10816LisbonShared room30
10917TokyoEntire home190
11012ParisEntire home240

Example Output

citynightsrevenue
Paris101830
Tokyo71120
Lisbon4480

Explanation

In the example, Paris had four completed stays: 2 nights at 210, 2 nights at 85, 5 nights at 240 and 1 night at 40, which adds up to 10 nights and 1,830 in revenue. A stay from June 1 to June 5 counts as 4 nights.

The example above is a small slice of the data. Your code runs against the full DataFrames.