2017-12-04 4 views
0

サンプル入力は:特定のIDに対して特定のイベントが発生した合計時間をカウントするために、集計でpandas groupbyを使用するにはどうすればよいですか?

ID  Timestamp  Event 
1 1 2010-05-31 13:43:45 a 
2 1 2010-05-31 13:44:25 a 
3 1 2010-05-31 13:44:55 a 
4 1 2010-05-31 13:45:35 b 
5 1 2010-05-31 13:47:05 b 
6 1 2010-05-31 13:47:45 a 
7 1 2010-05-31 13:49:45 c 
8 1 2010-05-31 13:50:45 b 
9 1 2010-06-01 09:20:00 c 
10 1 2010-06-01 09:22:00 c 
11 1 2010-06-01 09:23:20 c 
12 1 2010-06-01 09:24:00 a 
13 1 2010-06-01 09:24:15 a 
14 1 2010-06-01 09:25:00 b 
15 1 2010-06-01 09:25:42 b 
16 2 2010-05-31 11:25:38 a 
17 2 2010-05-31 11:26:00 c 
18 2 2010-05-31 11:26:45 a 
19 2 2010-05-31 11:27:10 a 
20 2 2010-05-31 11:27:37 a 
21 2 2010-05-31 11:29:35 b 
22 2 2010-05-31 11:30:25 b 
. .    .   . 
. .    .   . 
. .    .   . 

これは私が現在やっているの擬似コードであるが、これは、サンプル出力です

# Right now I'm looping through the dataframe, using this pseudocode: 
for row in df.itertuples(): 
    check for ID match: (say, 1 == 1) 
     check for event match: (say a == a) 
      calculate time duration, 
      check for < 3600 
       store in a dictionary of the events ('a', 'b',etc = keys), cumulative 
       store this events dictionary for the ID 
      else Increment session accordingly 
     else 
      store the previous occurrence time of the previous event, 
      use it for the next occurrence of the event, using a dictionary; 

      At the same time, find the current event's duration w.r.t. 
      the time stored at the previous changing of the event(in the above line), 
     again check for < 3600 
        store in a dictionary of the events ('a', 'b',etc = keys), cumulative 
        store this events dictionary for the ID 
     else Increment session accordingly 
    else 
     reset events dictionary to zero values 
     reset the event change dictionary to zero values 

正常に動作していないのが、私は何をしたいかに類似。

ID a_time b_time c_time Session 
1 1  120  200  100  2 
2 2  235  340  145  3 
. .  .  .  .  . 
. .  .  .  .  . 

Actually there are 7 unique events in all, I've just mentioned 3 
to keep it less complicated. 

{基本的に「a_time」=その発生の持続時間は、3600より大きくない場合、すべてのに要する時間の合計は、(「」現在のIDのイベントが出現箇所、他応じセッション数を増やしますセッション+時間の期間/ 3600、またはプラス1でのみだけでなく、日付が変更された場合)

「」=類似したエントリー}

希望: は、より多くのニシキヘビ、またはベクトル化方法はありますこれをする? 「

df.groupby(['ID'])['Timestamp'].diff() 

を使用して同じように私は、このバージョンの多くを使用しますが、私はdidnの場合は、私が

「pd.groupby用なし集計関数が利用できる」を取得するとして、それは、私のために動作しませんでしたtは、私はちょうど、各IDのためのユニークなイベントのそれぞれのカウントとなり、出力を得るために

df.groupby(['ID'])['Event'].value_counts().unstack().fillna(0) 

コマンドを使用したい時のログを使用する必要があります。

擬似コードアプローチを使用しての私の得られた出力は、以下に示す(ちょうどいくつかのランダムな数字がここで示され、計算されていない)1行目の単なる繰り返しである、と私はすべて見ることができないんだけどイベントだけでなく、わずか3:

[「タイムスタンプ」]とGROUPBYを使用して
 ID  a_time  b_time  c_time 
1  1   120   200   100 

デフ()上記の部分で述べたようには私にエラー「pd.groupbyのために利用可能な集計機能」を提供します。私はsort_valuesを使用しても適用しましたが、私はいつもこれを持っていました同じエラー(上記の行に記載されています)。

+0

ちょうど、私に知らせてくれてありがとう! – Darknorth

答えて

0

私はそれを自分で解決しました。

基本的に
df.at[index, col] = df.at[index, col] + duration 

、データフレームは、それゆえ、私はまだそれのセルでのデータフレームにアクセスすることができ、マトリックスとして可視化することができますので、私はそれを解決するために管理して:私はちょうどこれを使用することができることを考え出しました。