Reputation: 2253
I have df
i,Unnamed: 0,ID,active_seconds,subdomain,search_term,period,code,buy
0,56574,08cd0141663315ce71e0121e3cd8d91f,6,market.yandex.ru,None,515,100.0,1.0
1,56576,08cd0141663315ce71e0121e3cd8d91f,26,market.yandex.ru,None,515,100.0,1.0
2,56578,08cd0141663315ce71e0121e3cd8d91f,14,market.yandex.ru,None,515,100.0,1.0
3,56579,08cd0141663315ce71e0121e3cd8d91f,2,market.yandex.ru,None,515,100.0,1.0
4,56581,08cd0141663315ce71e0121e3cd8d91f,8,market.yandex.ru,None,515,100.0,1.0
5,56582,08cd0141663315ce71e0121e3cd8d91f,32,market.yandex.ru,None,515,100.0,1.0
6,56583,08cd0141663315ce71e0121e3cd8d91f,16,market.yandex.ru,None,515,100.0,1.0
7,56584,7602962fb83ac2e2a0cb44158ca88464,4,market.yandex.ru,None,515,100.0,2.0
8,56585,7602962fb83ac2e2a0cb44158ca88464,10,market.yandex.ru,None,515,100.0,2.0
9,56639,7602962fb83ac2e2a0cb44158ca88464,2,market.yandex.ru,None,516,100.0,2.0
I need to count sum of active_seconds
to every ID,
df.groupby(['ID', 'buy']).agg({'active_seconds': sum}).rename(columns={'active_seconds': 'count_sec'}).reset_index()
But I need do it, if buy == 2 or buy == 3
, if buy == 1
, I need to print date from this df.
ID date buy
7602962fb83ac2e2a0cb44158ca88464 01.01.2016 1
bc8a731e4c7e6f6b96e56ebe7f766bcd 10.02.2016 1
a703114aa8a03495c3e042647212fa63 20.02.2016 2
How can I do that?
Upvotes: 0
Views: 110
Reputation: 814
If I understand your question correctly, you want to join with a different data frame when buy == 1. Assuming the first data frame is named df and the second data frame that contains dates is named df2 then this is my proposed solution:
df.groupby(['ID', 'buy']).agg({'active_seconds': sum}).rename(columns={'active_seconds': 'count_sec'}).reset_index().merge(df2, how='left', on=['ID','buy']).apply(lambda x: x['date'] if x['buy']==1 else x['count_sec'],axis=1)
Upvotes: 1