【问题标题】:I need to save the output of Print command into a list我需要将打印命令的输出保存到列表中
【发布时间】:2020-01-02 03:49:08
【问题描述】:

我正在使用 pandas 来比较持续时间以标记出勤率。 我使用 DateTime 和 timedelta 来比较持续时间。我想将输出保存在相邻列中,以便我们知道谁在场,谁不在。

我可以比较值并打印此人是否在场,但我想在此人姓名前面的 excel 文件中执行此操作

df1 =df[['Date', 'Agent Name', 'Login Time']]

for x in df1['Login Time']:
s1 = str(x)
s2 = '12:00:00'
s3 = '3:10:00'
s4 = '01:00:00'
FMT = '%H:%M:%S'
tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
if x == s4:
    print('Aussi')
elif (str(tdelta)) < s3:
    print('Present')
else: 
    print('Check')

我的想法是,我会将输出的值存储在一个列表中,然后将该列表添加到 df 中的一个新列中。

【问题讨论】:

  • Aussi Aussi Aussi Aussi Aussi 现在 现在 现在 现在 检查 现在 现在 检查 现在 现在 现在 现在 检查 现在 现在 现在 现在 现在 现在 现在 现在 现在 现在 现在 现在 现在 现在 检查 检查 现在 现在 检查 现在 检查 现在 现在 现在 现在Present Present Present Present 这是输出
  • 你在for循环之前尝试attendence = []然后attendance.append(value),其中value可以是AussiePresent , 或 检查?
  • 非常感谢!成功了!
  • 酷豆,我添加了一个答案来正式确定您正在寻找的内容。如果您在我的回答中看到有价值的信息,您可以通过单击它旁边的复选标记来接受它。

标签: python python-3.x pandas loops if-statement


【解决方案1】:

您需要在循环之前创建一个空列表,然后在循环期间附加到它。

  1. for 循环之前添加:
attendance = []
  1. 将此添加到for 循环的最后,其中value 可以是AussiPresentCheck之一>:
attendance.append(value)

它可能是这样的:

df1 =df[['Date', 'Agent Name', 'Login Time']]

attendance = []
for x in df1['Login Time']:
    s1 = str(x)
    s2 = '12:00:00'
    s3 = '3:10:00'
    s4 = '01:00:00'
    FMT = '%H:%M:%S'
    tdelta = datetime.strptime(s2, FMT) - datetime.strptime(s1, FMT)
    if x == s4:
        value = 'Aussi'
    elif (str(tdelta)) < s3:
        value = 'Present'
    else: 
        value = 'Check'
    attendance.append(value)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2011-12-31
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多