- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要计算目录中包含多个电子表格的 n 个 excel 文件的时间差。
首先,我根据日期将数据框拆分为电子表格,然后检查 Door Name
列中是否有两个连续的行。是否不同,如果数据帧的长度是偶数,我最后计算了时间差。
第一步:
第二步:
我的代码:
import pandas as pd
import glob
import datetime
from tkinter import filedialog
pathEmp=Employees + "/*.xlsx" # Select directory using tkinter
for femp in glob.glob(pathEmp):
print('******\n')
name_file=os.path.split(femp)[-1]
print('Employee ',name_file)
xl = pd.ExcelFile(femp)
print('Sheet name: ',xl.sheet_names)
for sh in xl.sheet_names:
df = xl.parse(sh)
print('Processing: [{}] ...'.format(sh))
print('length : ',len(df))
df['Time'] = pd.to_datetime(df['Time'])
df['value'] = (df[['Door Name']] != df[['Door Name']].shift()).any(axis=1)
print('My df\n',df)
for i in range (len(df)):
if (len(df)) %2 == 0:
if (df.value.nunique() == 1):
df['Working hours'] = df['Time'].iloc[1::2].to_numpy() - df['Time'].iloc[::2]
Total = df['Working hours'].sum()
Total = '%02d:%02d:%02d' % (Total.days*24 + Total.seconds // 3600, (Total.seconds % 3600) // 60, Total.seconds // 60)
print('Working hours', Total)
Working hours
在我目录中每个excel文件的每个电子表格中?
最佳答案
这是一个例子。
# your input
df = pd.DataFrame({
'DoorName': ('RDC_IN-1', 'RDC_OUT-1', 'RDC_IN-1', 'RDC_OUT-1', 'RDC_IN-1', 'RDC_OUT-1',
'RDC_IN-1', 'RDC_OUT-1', 'RDC_IN-1', 'RDC_OUT-1', 'RDC_IN-1'),
'Time': (datetime(2019, 9, 30, 17, 49, 6), datetime(2019, 9, 30, 17, 45, 51),
datetime(2019, 9, 30, 17, 45, 28), datetime(2019, 9, 30, 16, 37, 53),
datetime(2019, 9, 30, 15, 59, 53), datetime(2019, 9, 30, 9, 15, 0),
datetime(2019, 9, 27, 18, 25, 39), datetime(2019, 9, 27, 18, 27, 9),
datetime(2019, 9, 27, 12, 10, 33),
datetime(2019, 9, 27, 8, 42, 50), datetime(2019, 9, 27, 18, 24, 34)),
})
df['name'] = 'Arya Stark'
# generate date column from Time column
df['date'] = df['Time'].dt.strftime('%Y-%m-%d')
# open file for writing
with pd.ExcelWriter('output.xlsx') as writer:
# for each unique date
for u_date in df['date'].unique(): # type: str
# sub DataFrame from main DataFrame by date
df_by_date = df[df['date'] == u_date]
# date column is no longer needed
df_by_date = df_by_date.drop(columns=['date'])
# DoorName Cumulative sum + group by name (Arya Stark)
s = df_by_date['DoorName'].eq('RDC_IN-1').iloc[::].cumsum()
con = df_by_date.name.groupby(s).transform('nunique') == 1
# diff in seconds between RDC_IN and RDC_OUT for each couple
sec_df = df_by_date[con].groupby(s).agg({
'Time': lambda x: (x.iloc[0] - x.iloc[-1]).seconds
})
df_by_date = df_by_date.reset_index()
df_by_date = df_by_date.drop(columns=['index'])
df_by_date['WorkingHours'] = ''
# sum all seconds and convert to timedelta
working_hours = str(timedelta(seconds=int(sec_df['Time'].sum())))
# insert only in first row of sheet(as in your example)
df_by_date['WorkingHours'].loc[0] = working_hours
# append sheet by unique date
df_by_date.to_excel(writer, sheet_name=u_date, index=False)
关于excel - 计算列中的时间差并将其保存为具有多个电子表格的许多 excel 文件,PYTHON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59307376/
我对 EPOCH 时间有疑问。 我需要计算两个数据包之间的时间差。我不太确定如何: printf("Epoch Time: %d:%d seconds\n", header->ts.tv_sec, h
我正在尝试了解数字旋转的速度。我有一个程序,其中计数器从 0 开始。 每次用户单击按钮“加 1”。它将计数器递增 1。 计数器最大值为 255。在 255 之后它回到 0。 现在我想显示从 0 到递增
基本上我想做的是根据 SIP 信令获取通话持续时间。 我有一个包含如下所示记录的表,我正在尝试编写一个返回以下内容的 SELECT 语句: id callid date
您好,我需要有关 MySQL 数据库查询的帮助。 我有一个看起来像这样的表: ID TRACKID DATE Name Action 38
细节。我有包含以下列的注释表。 ID - INT(3) Date - DateTime Note - VARCHAR(100) Tile - Varchar(100
午夜过后我很难计算时间: String time = "15:00-18:05"; //Calculating OK //String time = "22:00-01:05"; //Not
我使用 XML 从 MySQL 数据库中抓取了两个日期,如下所示。我想获得以秒为单位的时差。 我写了这个脚本,但它给出了“NaN” function show(){ var t1 = new Dat
如何在 MYSQL phpMyAdmin 数据库中以 (TIME 00:00:00) 格式更新另一个字段(整列)的时间差 当我在 PHP 中执行此操作时,我不断收到 0:00:00 并且没有结果。其
我的表单中有两个字段,用户可以在其中选择输入时间(开始时间、结束时间),我想在更改这些字段时重新计算另一个字段的值。 我想做的是获取 2 次之间的小时数。因此,例如,如果我的开始时间为 5:30,结束
我想使用 Date 和 Calendar 类计算 java 中两个日期/时间之间的差异。我的格式是“2012-01-24 12:30:00 PM”。 我已经实现了我自己的方法,也用谷歌搜索它以与其他人
MySQL 服务器上的访客数据库如下所示id 是主键整数类型,firstname 和 lastname 是文本类型,访客 id 是整数类型 id firstname lastname
我是一名优秀的程序员,十分优秀!