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"

import pandas as pd def find_us(val): if val.startswith('agg'): val = 'aggregators' elif val.startswith('vk_adv') or val.startswith('facebook_adv') or val.startswith('instagram_adv') or val.startswith('telegram_adv'): val = 'social' return val df = pd.read_csv('/datasets/how_find_us.csv') df['how_find_us'] = df['how_find_us'].apply(find_us) df['how_find_us'].value_counts()

Questions about programming?Chat with your personal AI assistant