2016-08-11 1 views
2

を作るためにGROUPBYを使用して、私は、データフレームパンダ:テーブル

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 

を持っている私は(ワン数は1回の周期である)active_secondsの合計とperiodの量を持つテーブルを取得したいです。この場合、このIDにピリオド= 2の数量を取得したいとします。 は私が

df.groupby(['ID', 'buy']).agg({'period': len, 'active_seconds': sum}).rename(columns={'active_seconds': 'count_sec', 'period': 'sum_session'}).reset_index() 

を使用しますが、それは期間の量にuncorrectly値を返します。どうすれば修正できますか?

答えて

1

代わりlen

df.groupby(['ID', 'buy']).agg({'period': 'nunique', 'active_seconds': sum}) \ 
    .rename(columns={'active_seconds': 'count_sec', 'period': 'sum_session'}).reset_index() 

enter image description here

の使用 'nunique'
関連する問題