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数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • postman传递当前时间戳实例详解

    postman传递当前时间戳实例详解

    在本篇文章里小编给大家整理的是一篇关于postman传递当前时间戳知识点相关内容,有需要的朋友们可以学习下。
    2019-09-09
  • 让你的Python代码实现类型提示功能

    让你的Python代码实现类型提示功能

    今天小编就为大家分享一篇让你的Python代码实现类型提示功能,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • Python多版本管理与pip升级的全面指南

    Python多版本管理与pip升级的全面指南

    在Python开发过程中,多版本共存、pip升级失败和环境变量冲突是常见问题,本文介绍了如何管理Python多版本和正确升级pip,感兴趣的小伙伴可以了解下
    2025-05-05
  • 详谈python中subprocess shell=False与shell=True的区别

    详谈python中subprocess shell=False与shell=True的区别

    这篇文章主要介绍了详谈python中subprocess shell=False与shell=True的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • python操作注册表的方法实现

    python操作注册表的方法实现

    Python提供了winreg模块,可以用于操作Windows注册表,本文就来介绍一下python操作注册表的方法实现,主要包括打开注册表、读取注册表值、写入注册表值和关闭注册表,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08
  • 深入理解python中的select模块

    深入理解python中的select模块

    这篇文章主要介绍了python中select模块的相关资料,Python中的select模块专注于I/O多路复用,提供了select、poll和epoll三个方法,文中还详细的介绍了select和poll,因为poll与select相差不大,所以本文不作介绍,需要的朋友们下面来一起看看吧。
    2017-04-04
  • 使用Python打造专业演示文稿转换器(Markdown转PPT)

    使用Python打造专业演示文稿转换器(Markdown转PPT)

    在日常工作中,我们经常需要将Markdown格式的文档转换为演示文稿,手动复制粘贴不仅繁琐,还容易出错,今天我们就来看看如何用Python开发一个功能完整的Markdown到PPTX转换器
    2025-07-07
  • Python八大常见排序算法定义、实现及时间消耗效率分析

    Python八大常见排序算法定义、实现及时间消耗效率分析

    这篇文章主要介绍了Python八大常见排序算法定义、实现及时间消耗效率分析,结合具体实例形式对比分析了冒泡排序、直接插入排序、选择排序、归并排序、希尔排序、桶排序、堆排序等排序算法的使用与执行效率,需要的朋友可以参考下
    2018-04-04
  • Python pip 常用命令汇总

    Python pip 常用命令汇总

    这篇文章主要介绍了Python pip 常用命令汇总,帮助大家更好的理解和使用pip命令,感兴趣的朋友可以了解下
    2020-10-10
  • Python从入门到精通之Hash函数的使用详解

    Python从入门到精通之Hash函数的使用详解

    Python提供了强大而灵活的Hash函数,用于在各种应用中实现数据存储、数据校验、加密等功能,下面将从入门到精通介绍Python中Hash函数的使用,感兴趣的可以了解一下
    2023-08-08

最新评论