Python Pandas读取Excel数据并根据时间字段筛选数据

 更新时间:2025年07月22日 10:05:06   作者:岫珩  
这篇文章主要为大家详细介绍了Python如何调用Pandas实现读取Excel数据并根据时间字段筛选数据,感兴趣的小伙伴可以跟随小编一起学习一下

1. 需求描述

现在有一个excel表格,其中包含设备字段device_id、最后使用时间字段end_time以及其他字段若干

需要将表格中的每个设备对应的最新的使用时间筛选出来,并在结果中根据最新时间筛选出4月和5月

对应的设备号列表

2. 读取excel表格

import pandas as pd

# 读取 Excel 文件
file_path = r"C:\Users\Downloads\file_record.xlsx"  # 替换为你的文件路径
df = pd.read_excel(file_path)
# 显示前几行数据
# print(df.head())
# print(df)

3. 筛选最新时间

先根据时间重置DataFrame对象

# Assuming 'df' is your DataFrame and 'end_time' is initially in string format
df['end_time'] = pd.to_datetime(df['end_time'])  # Convert to datetime if necessary

然后根据设备号分组,再取end_time中最新即最大时间值,并重置索引

# Group by 'device_id' and find the max (latest) 'end_time' for each group
latest_end_times = df.groupby('device_id')['end_time'].max().reset_index()

4. 筛选具体月份数据

在上面的最新时间中筛选出4月和5月的设备列表

# Filter the 'latest_end_times' DataFrame to only include devices with 'end_time' in April or May
filtered_devices = latest_end_times[
    (latest_end_times['end_time'].dt.month == 4) | 
    (latest_end_times['end_time'].dt.month == 5)
]

5.输出结果

遍历结果中设备和时间信息

for index, row in filtered_devices.iterrows():
    device_id = row['device_id']
    latest_end_time = row['end_time']
    print(f"Device ID: {device_id}, Latest End Time: {latest_end_time}")


# 'filtered_devices' now contains the device information for which the latest 'end_time' is in April or May

6. 完整代码

完整代码如下

import pandas as pd

# 读取 Excel 文件
file_path = r"C:\Users\Downloads\file_record.xlsx"  # 替换为你的文件路径
df = pd.read_excel(file_path)

# 显示前几行数据
# print(df.head())
# print(df)

# Assuming 'df' is your DataFrame and 'end_time' is initially in string format
df['end_time'] = pd.to_datetime(df['end_time'])  # Convert to datetime if necessary
# print(df.head())

# Group by 'device_id' and find the max (latest) 'end_time' for each group
latest_end_times = df.groupby('device_id')['end_time'].max().reset_index()
# print(df)


# Filter the 'latest_end_times' DataFrame to only include devices with 'end_time' in April or May
filtered_devices = latest_end_times[
    (latest_end_times['end_time'].dt.month == 4) | 
    (latest_end_times['end_time'].dt.month == 5)
]

for index, row in filtered_devices.iterrows():
    device_id = row['device_id']
    latest_end_time = row['end_time']
    print(f"Device ID: {device_id}, Latest End Time: {latest_end_time}")


# 'filtered_devices' now contains the device information for which the latest 'end_time' is in April or May

到此这篇关于Python Pandas读取Excel数据并根据时间字段筛选数据的文章就介绍到这了,更多相关Pandas读取Excel数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python轻松实现图片文字提取的高效技巧分享

    Python轻松实现图片文字提取的高效技巧分享

    随着数字化转型的加速,从图片中提取文字(OCR,光学字符识别)的需求日益增长,Python凭借其丰富的库和易用性,成为实现OCR的首选工具之一,本文将深入探讨如何利用Python从图片中提取文字,涵盖基本原理、常用工具、代码实现及优化技巧,需要的朋友可以参考下
    2025-07-07
  • Python使用FastAPI+FastCRUD自动生成API接口的实现

    Python使用FastAPI+FastCRUD自动生成API接口的实现

    FastCRUD 是一个为 FastAPI + SQLAlchemy 场景设计的开源库,由 Benav Labs 维护,旨在简化 CRUD 端点搭建与数据库操作的流程,本文呢给大家介绍了Python使用FastAPI+FastCRUD自动生成API接口的实现,需要的朋友可以参考下
    2025-11-11
  • Python使用socket_TCP实现小文件下载功能

    Python使用socket_TCP实现小文件下载功能

    这篇文章主要介绍了Python使用socket_TCP实现小文件下载功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • Python Final 类型限定符详解

    Python Final 类型限定符详解

    Python的Final是一种类型限定符,用于标记变量、属性或函数参数,防止重新赋值,限制类的继承和方法重写,提高代码质量和可维护性,具有一定的参考价值,感兴趣的可以了解一下
    2026-05-05
  • Python使用腾讯云API实现短信验证码功能

    Python使用腾讯云API实现短信验证码功能

    使用Python与腾讯云接口对接,实现短信验证码功能变得非常简单,只需要几行代码就能够轻松实现短信的发送,无须关心复杂的短信协议和底层实现,读者可以根据自己的实际需求,灵活使用腾讯云短信SDK提供的API来实现更丰富的短信功能
    2024-01-01
  • pandas解决数据缺失、重复的方法与实践过程

    pandas解决数据缺失、重复的方法与实践过程

    这篇文章主要介绍了pandas解决数据缺失、重复的方法与实践过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • 记一次pyinstaller打包pygame项目为exe的过程(带图片)

    记一次pyinstaller打包pygame项目为exe的过程(带图片)

    这篇文章主要介绍了记一次pyinstaller打包pygame项目为exe的过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • arcgis使用Python脚本进行批量截图功能实现

    arcgis使用Python脚本进行批量截图功能实现

    最近公司数据部那边有个需求,需要结合矢量数据和影像数据,进行批量截图,并且截图中只能有一个图斑,还要添加上相应的水印,这篇文章主要介绍了arcgis使用Python脚本进行批量截图,需要的朋友可以参考下
    2023-01-01
  • Python中with及contextlib的用法详解

    Python中with及contextlib的用法详解

    这篇文章主要介绍了Python中with及contextlib的用法,结合实例形式较为详细的分析了with及contextlib的功能、使用方法与相关注意事项,需要的朋友可以参考下
    2017-06-06
  • python3获取两个日期之间所有日期,以及比较大小的实例

    python3获取两个日期之间所有日期,以及比较大小的实例

    下面小编就为大家分享一篇python3获取两个日期之间所有日期,以及比较大小的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04

最新评论