Generation

generate functionFri, 27 Jan 2023

There is a Pandas dataframe: date hotel revenue av_revenue difference in_percent 5 2021-07-01 Alpina 38736 NaN NaN NaN 11 2021-07-02 Moreon 40766 NaN NaN NaN 17 2021-07-03 Alpina 30330 NaN NaN NaN 23 2021-07-04 Galaxia 50180 NaN NaN NaN 29 2021-07-05 Optima 27006 NaN NaN NaN 1) Write a function that calculates the average revenue for each hotel and adds these values to the "av_revenue" column 2) Write a function that calculates the difference between the average revenue and the values in the "revenue" column and adds these calculations to the "difference" column 3) Write a function that calculates the difference between the average revenue and the values in the "revenue" column as a percentage and adds those calculations to the "in_percent" column

def av_revenue(df): df['av_revenue'] = df.groupby('hotel')['revenue'].transform('mean') return df def difference(df): df['difference'] = df['av_revenue'] - df['revenue'] return df def in_percent(df): df['in_percent'] = 100 * df['difference'] / df['av_revenue'] return df

Questions about programming?Chat with your personal AI assistant