could you speed up my code? создадим список датафреймов petsvsnests = [] для каждого,…
- User
could you speed up my code?
создадим список датафреймов
pets_vs_nests = []
для каждого, кто заходил в петов 3 раза и более и покупал гнездо
for i in tqdm(pets_basic[pets_basic.nest_buyer].appsflyer_id.unique()):
code# создадим свой датафрейм df = pets_n_nests[pets_n_nests.appsflyer_id == i].reset_index(drop=True) # сохраним кол-во открытий петов до первой покупки гнезда opens_before_buy = df[df.event_value.str.contains('UnlockNest')].index.min() # сохраним кол-во дней до первой покупки гнезда days_before_buy = (df[df.event_value.str.contains('UnlockNest')].event_time.dt.date.min() - df.event_time.dt.date.min()).days # соберем датафрейм с данными по игроку player = pd.DataFrame({'appslfyer_id': i, 'opens_before_buy': opens_before_buy, 'days_before_buy': days_before_buy}, index=[0]) # добавим его в общий список pets_vs_nests.append(player)объединим всех игроков в один датафрейм
pets_vs_nests = pd.concat(pets_vs_nests)
- Assistant
Most of the time-consuming operations in your code are executed within a loop. Here are a few optimizations you can make to potentially speed up your code:
-
Use a dictionary instead of a list to store the dataframes: Instead of appending each player's dataframe to the list
pets_vs_nests, create a dictionary where the player'sappsflyer_idis the key and the dataframe is the value. This will eliminate the need to concatenate dataframes at the end.
-