Reputation: 2253
I have dataframe
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,08cd0141663315ce71e0121e3cd8d91f,4,market.yandex.ru,None,515,100.0,1.0
8,56585,08cd0141663315ce71e0121e3cd8d91f,10,market.yandex.ru,None,515,100.0,1.0
9,56639,08cd0141663315ce71e0121e3cd8d91f,2,market.yandex.ru,None,516,100.0,1.0
I want to get table with sum of active_seconds
and quantity of period
(One number is a One period). In this case I want to get quantity of periods = 2
to this ID.
I use
df.groupby(['ID', 'buy']).agg({'period': len, 'active_seconds': sum}).rename(columns={'active_seconds': 'count_sec', 'period': 'sum_session'}).reset_index()
But it return uncorrectly value to quantity of periods. How can I fix that?
Upvotes: 2
Views: 3800
Reputation: 294338
Use 'nunique'
instead of len
df.groupby(['ID', 'buy']).agg({'period': 'nunique', 'active_seconds': sum}) \
.rename(columns={'active_seconds': 'count_sec', 'period': 'sum_session'}).reset_index()
Upvotes: 2