各訪問数をユーザー ID ごとに要約する新しいテーブルを作成する以下の SQL クエリがあります。このデータフレームをPythonで作成するにはどうすればよいですか?
create table User_Visits_summary as
select user_id,
sum(case when visit_type = 1 then 1 else 0 end) as Type_One_Counts,
sum(case when visit_type = 2 then 1 else 0 end) as Type_Two_Counts,
sum(case when visit_type = 3 then 1 else 0 end) as Type_Three_Counts,
count(*) as Total_Visits
from user_visits
group by user_id
以下のコードは、SQL クエリと同じテーブルを作成する必要があります。コード内のコメントを読み、デバッグ モードで実行して、コードの各行の機能をよりよく理解してください。Pandas の機能に関する便利なガイドについては、このチートシートをチェックしてください-
https://github.com/pandas-dev/pandas/blob/master/doc/cheatsheet/Pandas_Cheat_Sheet.pdf
import pandas as pd
# example dataset
user_visits = pd.DataFrame({'user_id' :['A','A','A','A','A','B','B','B','B'],
'visit_type':[ 1, 1, 3, 3, 3, 2, 2, 2, 2] })
# This summary table already contains the data you want, but on 'long column' format
User_Visits_summary = user_visits.groupby(['user_id','visit_type']).size().reset_index()
# Here we pivot the table to get to your desired format
User_Visits_summary = User_Visits_summary.pivot(index='user_id',columns='visit_type', values=0)
# Calculate total from sub-totals in new column
User_Visits_summary['Total_Visits'] = User_Visits_summary.sum(axis=1)
# Some formatting
User_Visits_summary.reset_index(inplace=True)
User_Visits_summary.rename(columns={1:'Type_One_Counts',
2:'Type_Two_Counts',
3:'Type_Three_Counts'}, inplace=True)
# Table ready
print(User_Visits_summary)
# ...too wide to paste...
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加