2016-06-20 5 views
0

私は、SaaSウィジェットのユーザーに関するAPIからいくつかのデータをストリーミングしており、プロセスの効率性を見極めるために「ユーザーの活動」に基づいて分析したい。私は、「何(グループ)のユーザー行動が成功裏に完了するか」などの質問に答えることを望んでいます。ストリーミングデータをフィーチャーテーブルに変換する - ストリーミングデータを分析する

現在のところ、データは、特定のユーザーに関するカテゴリの機能を含む応答のタイムスタンプログです。その特定の相互作用の期間のための具体的なアクションと応答:

Timestamp User Cat1 Cat2  Action  Response 
timenow  User1 False barbar action1 response4 
time(n-1) User2 False No value action1 response3 
time(n-2) User1 False barbar baraction response2 
time(n-3) User3 True bar  action1 response1 
time(n-4) User2 False foo  action1 response2 
time(n-5) User1 False barbar fooaction response1 

私は、ユーザーがグループにデータを好きで、それからカウントを持つすべてのアクションをリストしたい:

User Cat1 Cat2  Action1 Action2  Response1 Response 2 
User3 True bar  2   1   7   1 
User2 False foo  4   5   8   4 
User1 False barbar 5   2   3   0 

私はこれをやって想像することができますアウトパンダ、ループを使って新しい後の形式のデータフレーム。しかし、私はパンダの中でこれをやっていくためのすてきな方法があるのか​​、それとも似たような結果をもたらすかもしれないより良いフォーマットがあるのか​​どうか疑問に思っていますか?

答えて

1

私はあなたの出力を完全に理解していません。タイムスタンプ列はどこにありますか? Cat1Cat2の値をどのように選択しますか?

あなたはget_dummiesgroupbyを使用することができ、残りについては:入力データフレームを作成

import io 
temp = u"""Timestamp User Cat1 Cat2  Action  Response 
timenow  User1 False barbar action1 response4 
time(n-1) User2 False Novalue action1 response3 
time(n-2) User1 False barbar baraction response2 
time(n-3) User3 True bar  action1 response1 
time(n-4) User2 False foo  action1 response2 
time(n-5) User1 False barbar fooaction response1""" 
df = pd.read_csv(io.StringIO(temp),delim_whitespace = True) 

出力:get_dummies

Timestamp User Cat1 Cat2 Action  Response 
0 timenow  User1 False barbar action1  response4 
1 time(n-1) User2 False Novalue action1  response3 
2 time(n-2) User1 False barbar baraction response2 
3 time(n-3) User3 True bar  action1  response1 
4 time(n-4) User2 False foo  action1  response2 
5 time(n-5) User1 False barbar fooaction response1 

希望の列を取得する:

df = df[['User','Action','Response']] 
df = pd.concat([df,df['Action'].str.get_dummies(),df['Response'].str.get_dummies()],axis = 1) 
df.drop(['Action','Response'],1,inplace = True) 


    User action1 baraction fooaction response1 response2 response3 response4 
0 User1 1  0   0   0   0   0   1 
1 User2 1  0   0   0   0   1   0 
2 User1 0  1   0   0   1   0   0 
3 User3 1  0   0   1   0   0   0 
4 User2 1  0   0   0   1   0   0 
5 User1 0  0   1   1   0   0   0 

そして最後に、あなたがgroupbyを使用します。

df.groupby('User',as_index = False).sum() 

    User action1 baraction fooaction response1 response2 response3 response4 
0 User1 1  1   1   1   1   0   1 
1 User2 2  0   0   0   1   1   0 
2 User3 1  0   0   1   0   0   0 
+0

ysearkaこんにちは - このケースでは、私は別の形式でデータを取得するためにタイムスタンプを犠牲にしています – TMrtSmith

関連する問題