如何通过比较两个日期来打印矩阵

问题描述 投票:0回答:1

我有一个 Excel 工作表,有两列的值采用“dd-mm-yyyy hh:mm”格式,即 DEP_DATE_TIME(F 列)和 ARR_DATE_TIME(J 列)。行具有相同的“dd-mm-yyyy hh:mm”格式进行比较。 Please view this image for more details

我将 Col F1 与 Col L1 进行比较,如果为 TRUE,则它将返回 1,如果 Col J1 >= L1,则它将返回 0,否则返回 1,并通过减去这些结果将结果打印在相应的单元格中。以下是相同的公式 -

=IF($F$13=L1,0,1)

如何使用 python 代码实现相同的目的,以便它应该读取工作表并在单元格中绘制 0 和 1?

如何使用 python 代码实现相同的目的,以便它应该读取工作表并在单元格中绘制 0 和 1?

python python-3.x excel matrix excel-formula
1个回答
0
投票

import pandas as pd

# Load the Excel file into a pandas DataFrame
df = pd.read_excel("your_file.xlsx")

# Assuming your dates are in columns F and L, and column J for comparison
dep_date_time = pd.to_datetime(df['DEP_DATE_TIME'], format='%d-%m-%Y %H:%M')
arr_date_time = pd.to_datetime(df['ARR_DATE_TIME'], format='%d-%m-%Y %H:%M')
comparison_date = pd.to_datetime(df['L1'], format='%d-%m-%Y %H:%M')

# Apply your logic to create a new column with 0s and 1s
df['RESULT'] = (dep_date_time < comparison_date).astype(int) - (arr_date_time >= comparison_date).astype(int)

# Save the updated DataFrame back to Excel
df.to_excel("output_file.xlsx", index=False)


© www.soinside.com 2019 - 2024. All rights reserved.