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

import pandas as pd def divide_hotels(df): big_hotels = [] medium_hotels = [] small_hotels = [] for item in df['total_rooms']: if item > 30: big_hotels.append(df['profit']) elif item > 20: medium_hotels.append(df['profit']) elif item > 10: small_hotels.append(df['profit']) df['big_hotels'] = big_hotels df['medium_hotels'] = medium_hotels df['small_hotels'] = small_hotels return df

Questions about programming?Chat with your personal AI assistant