如何在 Python 中编写一个程序,通过使用 > 和 <, which will return 0 and 1.
等运算符来比较两个时间戳“dd-mm-yyyy hh:mm”正如您在上面我尝试导入的 Excel 工作表中看到的,我想将 A2 与 C1 进行比较。例如,如果 A2>C1 对所有行打印 1 else 0 - 并在 C2 中打印。但似乎每当我试图实现它时,要么打印全 0 或 1 或 NaN,这是错误的。
以下是我尝试过的代码:
import pandas as pd
import numpy as np
# Read the Excel sheet into a pandas DataFrame
df = pd.read_excel('MAT_TEST3.xlsx')
# Convert date and time columns to datetime objects using positional index
df.iloc[:, 0] = pd.to_datetime(df.iloc[:, 0], format='%d-%m-%Y %H:%M', errors='coerce')
df.iloc[:, 1] = pd.to_datetime(df.iloc[:, 1], format='%d-%m-%Y %H:%M', errors='coerce')
df.iloc[:, 2] = pd.to_datetime(df.iloc[:, 2], format='%d-%m-%Y %H:%M', errors='coerce')
# Compare dates and times and create a new column at the last position
df.iloc[:, 2] = (df.iloc[:, 0] > df.iloc[:, 2]).astype(int)
print(df)
# Save the modified DataFrame back to Excel
#df.to_excel('MAT_TEST3_OP.xlsx', index=False)
输出-
DEP_DATE_TIME ARR_DATE_TIME 2024-01-05 03:59:59.545000 \
0 2024-01-05 03:13:00 2024-01-05 11:23:00 0
1 2024-01-06 02:35:00 2024-01-06 11:30:00 0
2 2024-01-07 02:35:00 2024-01-07 11:30:00 0
3 2024-01-05 14:30:00 2024-01-06 00:25:00 0
4 2024-01-06 14:30:00 2024-01-07 00:25:00 0
5 2024-01-07 14:30:00 2024-01-08 00:25:00 0
2024-01-05 04:59:59.580000 2024-01-05 05:59:59.615000 \
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN NaN
5 NaN NaN
2024-01-05 06:59:59.650000
0 NaN
1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
无需摆弄
.iloc
,可以直接使用列标题
有选择地迭代它们,你可以做这样的事情
# convert contents of columns to dates
for col_name in ["DEP_DATE_TIME", "ARR_DATE_TIME"]:
df[col_name] = pd.to_datetime(df[col_name])
# now slice and iterate over later columns
for col_name in df.columns[2:]:
# convert column name to datetime
date_base = pd.to_datetime(col_name)
df[col_name] = (df["DEP_DATE_TIME"] > date_base).astype(int)
您可以使用 openpyxl
轻松完成此操作精确的机制取决于日期/时间单元格的格式。如果它们的格式为日期/时间,那么 openpyxl 将返回单元格值作为 datetime.datetime,在这种情况下,您可以按如下方式进行直接比较:
import openpyxl
XL = "MAT_TEST3.xlsx"
ws = (wb := openpyxl.load_workbook(XL)).active
ws["C2"] = 1 if ws["A2"].value > ws["C1"].value else 0 # type: ignore
wb.save(XL)
如果值是文本,那么你应该查看 strptime