Generation

generate functionSat, 28 Jan 2023

There is a Pandas dataframe: date city hotel total_rooms ocup_rooms revenue oper_costs adv_costs profit 0 2021-07-01 Yalta Rapsodia 33 24 78936 25641 19305 33990 1 2021-07-01 Yalta AquaMania 28 14 51156 21756 16380 13020 2 2021-07-01 Yalta Skyline 15 12 38016 11655 8775 17586 3 2021-07-01 Alushta Breeze 36 25 75475 27972 21060 26443 4 2021-07-01 Alushta Moreon 20 17 51091 15540 11700 23851 5 2021-07-01 Gurzuf Alpina 14 12 38736 10878 8190 19668 Write a function divide_hotels that creates new columns big_hotels, medium_hotels, small_hotels, and adds values according to the following conditions: 1) if df['total_rooms'] > 30, then profit value is substituted into big_hotels column 2) if df['total_rooms'] > 20, then the profit value is substituted in the medium_hotels column 3) if df['total_rooms'] > 10, then profit value is substituted for small_hotels column

def divide_hotels(df): df['big_hotels'] = df.apply(lambda x: x['profit'] if x['total_rooms'] > 30 else 0, axis=1) df['medium_hotels'] = df.apply(lambda x: x['profit'] if x['total_rooms'] <= 30 and x['total_rooms'] > 20 else 0, axis=1) df['small_hotels'] = df.apply(lambda x: x['profit'] if x['total_rooms'] <= 20 and x['total_rooms'] > 10 else 0, axis=1) return df df = divide_hotels(df) df.head()

Questions about programming?Chat with your personal AI assistant