Generation

generate functionFri, 27 Jan 2023

There is a Pandas dataframe: hotel how_find_us Alpina agg_101hotels.com 2 agg_airbnb.com 4 agg_booking.com 3 agg_bronevik.com 1 agg_hotellook.ru 4 agg_level.travel 2 agg_onetwotrip.com 2 agg_onlinetours.ru 2 agg_ostrovok.ru 1 agg_other_sites 1 agg_roomguru.ru 2 agg_sutochno.ru 6 agg_travelata.ru 3 agg_tripadvisor.ru 3 agg_trivago.com 6 agg_tvil.ru 3 agg_yandex_travel 5 by_recommendation 8 facebook_adv 1 google_adv 1 instagram_adv 6 outdoor 6 regular_customer 1 seo 5 social 3 telegram_adv 2 tour_agents 3 unknown 4 vk_adv 3 yandex_adv 10 Write a function that creates a new dataframe by grouping the values in the "how_find_us" column according to the following rules: 1) if there are "agg" in the row, replace all row values with "aggregators" 2) if there are words "facebook" or "vk" or "instagram" or "telegram" in the line, replace these values with "social"

def find_us(df): df.loc[df.hotel=='Alpina', 'how_find_us'].value_counts() df.loc[df.hotel=='Alpina', 'how_find_us'] = df.loc[df.hotel=='Alpina', 'how_find_us'].map(lambda x: 'aggregators' if 'agg' in x else x) df.loc[df.hotel=='Alpina', 'how_find_us'] = df.loc[df.hotel=='Alpina', 'how_find_us'].map(lambda x: 'social' if 'facebook' in x or 'vk' in x or 'instagram' in x or 'telegram' in x else x) return df

Questions about programming?Chat with your personal AI assistant