You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Pandas根据当日日期提取Excel课程表对应行数据

Fixing the Date Filter for Your Pandas Schedule DataFrame

Hey there! Let's get that daily schedule filtering working properly. The key issue here is making sure the date formats in your DataFrame match the date.today() output, so Pandas can correctly compare them. Here's a step-by-step solution:

1. Ensure Your Date Column is in the Right Format

First, double-check that your DATE column is converted to a date-only type (not datetime with time, or a string). If you haven't already done this correctly, add this line after reading your Excel file:

import pandas as pd
from datetime import date

# Read the Excel file (you probably have this already)
df = pd.read_excel("your_schedule.xlsx", parse_dates=["DATE"])

# Convert DATE column to date-only type to match date.today()
df["DATE"] = df["DATE"].dt.date

2. Filter for Today's Schedule

Now, grab today's date and filter the DataFrame. We'll also add a check in case there are no classes scheduled for today:

# Get today's date
today_date = date.today()

# Filter the dataframe for rows where DATE matches today
today_classes = df[df["DATE"] == today_date]

# Print the result neatly
if not today_classes.empty:
    print("Today's Classes:\n")
    print(today_classes.to_string(index=False))  # Hides the index for cleaner output
else:
    print("No classes scheduled for today! 🎉")

Common Mistakes to Avoid

  • Mismatched Data Types: If your DATE column is stored as a string (e.g., "2024-05-20") instead of a date object, the comparison df["DATE"] == today_date will fail. Always convert date columns to proper date/datetime types first.
  • Datetime with Time: If your DATE column includes time values (like 2024-05-20 00:00:00), use df["DATE"].dt.date to extract just the date part before comparing.

This should give you the single row (or rows, if multiple entries exist for the same date) of today's courses, formatted cleanly.

内容的提问来源于stack exchange,提问作者user3137708

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:12:39