当前位置: 代码迷 >> python >> 熊猫数据框中的每日总活动时间
  详细解决方案

熊猫数据框中的每日总活动时间

热度:36   发布时间:2023-07-16 11:03:50.0

我是python的新手,我的英语不太好,所以我将尝试在下面的示例中解释我的问题。

In :ds # is my dataframe
Out :DateStarted       DateCompleted  DayStarted DayCompleted  \
1460  2017-06-12 14:03:32 2017-06-12 14:04:07  2017-06-12   2017-06-12   
14445 2017-06-13 13:39:16 2017-06-13 13:40:32  2017-06-13   2017-06-13   
14109 2017-06-21 10:25:36 2017-06-21 10:32:17  2017-06-21   2017-06-21   
16652 2017-06-27 15:44:28 2017-06-27 15:44:41  2017-06-27   2017-06-27   
30062 2017-07-05 09:49:01 2017-07-05 10:04:00  2017-07-05   2017-07-05   
22357 2017-08-31 09:06:00 2017-08-31 09:10:31  2017-08-31   2017-08-31   
39117 2017-09-08 08:43:07 2017-09-08 08:44:51  2017-09-08   2017-09-08   
41903 2017-09-15 12:54:40 2017-09-15 14:00:06  2017-09-15   2017-09-15   
74633 2017-09-27 12:41:09 2017-09-27 13:16:04  2017-09-27   2017-09-27   
69315 2017-10-23 08:25:28 2017-10-23 08:26:09  2017-10-23   2017-10-23   
87508 2017-10-30 12:19:19 2017-10-30 12:19:45  2017-10-30   2017-10-30   
86828 2017-11-03 12:20:09 2017-11-03 12:24:56  2017-11-03   2017-11-03   
89877 2017-11-06 13:52:05 2017-11-06 13:52:50  2017-11-06   2017-11-06   
94970 2017-11-07 08:09:53 2017-11-07 08:10:15  2017-11-07   2017-11-07   
94866 2017-11-28 14:38:14 2017-11-30 07:51:04  2017-11-28   2017-11-30   

       DailyTotalActiveTime      diff  
1460                    NaN      35.0  
14445                   NaN      76.0  
14109                   NaN     401.0  
16652                   NaN      13.0  
30062                   NaN     899.0  
22357                   NaN     271.0  
39117                   NaN     104.0  
41903                   NaN    3926.0  
74633                   NaN    2095.0  
69315                   NaN      41.0  
87508                   NaN      26.0  
86828                   NaN     287.0  
89877                   NaN      45.0  
94970                   NaN      22.0  
94866                   NaN  148370.0  

在DailyTotalActiveTime列中,我想计算多少时间,
特定日期总计。 diff列以秒为单位。
我试过了,但是没有结果:

for i in ds['diff']:
if i <= 86400:
    ds['DailyTotalActiveTime']==i
else:
    ds['DailyTotalActiveTime']==86400
    ds['DailyTotalActiveTime']+1 == i-86400

我能做什么? 再次,抱歉为您解释。

您应该尝试使用=而不是==

为了使您走到一半,您可以执行以下操作(我相信必须有一种更简单的方法,但是我现在看不到它):

df['datestarted'] = pd.to_datetime(df['datestarted'])
df['datecompleted'] = pd.to_datetime(df['datecompleted'])

df['daystarted'] = df['datestarted'].dt.date
df['daycompleted'] = df['datecompleted'].dt.date

df['Date'] = df['daystarted'] # This is the unqiue date per row.

for row in df.itertuples():
    if (row.daycompleted - row.daystarted) > pd.Timedelta(days=0):
        for i in range(1, (row.daycompleted - row.daystarted).days+1):
            df2 = pd.DataFrame([row]).drop('Index', axis=1)
            df2['Date'] = df2['Date'] + pd.Timedelta(days=i)
            df = df.append(df2)
  相关解决方案