Want to get involved? We're always looking for ideas and content for Weekly Challenges.
SUBMIT YOUR IDEAAlternative solution!
#################################
from ayx import Package
from ayx import Alteryx
import pandas as pd
# Input Alteryx hotel bookings data
df = Alteryx.read("#1")
#################################
# Filter for only those records with reservation status = "Check-Out"
df = df.query('reservation_status == "Check-Out"')
# Format date fields for arrival date and checkout date, and calculate # of days between them
df['arrival_date'] = pd.to_datetime(df['arrival_date_year']+"-"+df['arrival_date_month']+"-"+df['arrival_date_day_of_month'], infer_datetime_format='True')
df['checkout_date'] = pd.to_datetime(df['reservation_status_date'])
df['DaysStayed'] = (df['checkout_date'] - df['arrival_date']).dt.days
df['Number of Reservations'] = 1
# Filter for only those reservations with a duration > 0 days, and format columns
df = df[['arrival_date_month','DaysStayed','Number of Reservations']] \
.query("DaysStayed != '0'") \
.rename(columns = {'arrival_date_month':'Month', 'DaysStayed':'Avg Length of Stay'})
#################################
# Create aggregations of booking data for Avg Length of Stay and Number of Reservations
hotel_bookings = df.groupby('Month', as_index = False).agg({'Avg Length of Stay': "mean", 'Number of Reservations':"sum"})
#################################
Alteryx.write(hotel_bookings,1)
#################################
Cheers!
NJ
Nice quick one, could shorten it by removing the first filter and the 2nd will take care of the canceled anyway...