【发布时间】:2022-01-11 12:34:53
【问题描述】:
我有两张表需要合并两张表并获取用户的登录时间和注销时间以及平均时间`
表名:AllLogin
id login_date login_time user_id
1 2021-12-29 21:45:42.061506 4
2 2021-12-30 15:47:56.740154 2
3 2021-12-30 19:50:20.742582 4
4 2021-12-30 19:55:41.187090 1
5 2022-01-03 17:31:53.078602 1
6 2022-01-03 17:32:31.224342 2
7 2022-01-03 19:12:03.104795 4
8 2022-01-04 15:40:22.111702 1
9 2022-01-05 16:07:42.412031 2
10 2022-01-05 16:07:57.733591 2
11 2022-01-05 16:08:31.979159 1
表名:AllLogout
id logout_date logout_time user_id
1 2022-01-06 14:51:14.752459 1
2 2022-01-06 14:51:59.060639 2
3 2022-01-10 13:48:54.729281 2
4 2022-01-10 14:04:54.520387 2
5 2022-01-10 14:05:31.098456 2
如何组合两张表,统计每天用户的平均时间, 用户多次登录和注销也多次我需要用户每天的平均时间 `
代码::
queryset=AllLogin.objects.values('login_time')
querySet3 = AllLogout.objects.values('logout_time')
df1 = pd.DataFrame(queryset, columns = ['login_time','user'])
df2 = pd.DataFrame(querySet3, columns = ['logout_time','user'])
# print(df1,df2)
# df_row = pd.concat([df1, df2], ignore_index=True)
df = pd.concat([df1.reset_index(drop=True),df2.reset_index(drop=True)], axis=1)
# means = df.groupby(pd.Grouper(freq='1D')).mean()
df_row_reindex = pd.concat([df1, df2], ignore_index=True)
df=pd.merge(df1, df2, on=['user']).set_index(['logout_time','user']).sum(axis=1)
# df = (pd.to_datetime(df.logout_time) - pd.to_datetime(df.login_time)).dt.total_seconds()
'''changed'''
df['AllLogin_flag'] = 'AllLogin'
df['AllLogout_flag'] = 'AllLogout'
logout =df.rename(columns={'logout_date':'login_date','logout_time':'login_time'})
# login1 = login.append(logout, ignore_index=True)
# login1 = login1.sort_values(by=['user_id', 'login_date','login_time'])
【问题讨论】:
-
将您的数据发布为数据框构造函数,例如
df = pd.DataFrame({"user_id": [1, 2, 3, etc..]})
标签: python pandas database dataframe pandas-groupby