我有下面的代码,它将日期列与给定日期进行比较,我希望在满足条件时得到输出,即日期列中的日期在给定日期之后。任何帮助将不胜感激。谢谢
import pandas as pd
df1=pd.read_excel('test_date.xlsx',engine='openpyxl')
start_date = pd.Timestamp(2024, 5, 1)
filtered_date = df1.loc[df1['date'] > start_date]
filtered_date.to_csv("filtered.csv",index=False)
预期输出:
代码
首先将您的
date
列转换为日期时间数据类型,然后设置条件。
import pandas as pd
df1 = pd.read_excel('test_date.xlsx',engine='openpyxl')
cond = pd.to_datetime(df['date'], format='%d/%m/%Y %H:%M', errors='coerce') > pd.Timestamp(2024, 5, 1)
filtered_date = df1.loc[cond]
filtered_date.to_csv("filtered.csv",index=False)