当前位置: 首页 > news >正文

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

🤟致敬读者

  • 🟩感谢阅读🟦笑口常开🟪生日快乐⬛早点睡觉

📘博主相关

  • 🟧博主信息🟨博客首页🟫专栏推荐🟥活动信息

文章目录

  • Python Pandas读取Excel表格中数据并根据时间字段筛选数据
    • 1. 需求描述
    • 2. 读取excel表格
    • 3. 筛选最新时间
    • 4. 筛选具体月份数据
    • 5.输出结果
    • 6. 完整代码


📃文章前言

  • 🔷文章均为学习工作中整理的笔记。
  • 🔶如有错误请指正,共同学习进步。

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

📜文末寄语

  • 🟠关注我,获取更多内容。
  • 🟡技术动态、实战教程、问题解决方案等内容持续更新中。
  • 🟢《全栈知识库》技术交流和分享社区,集结全栈各领域开发者,期待你的加入。
  • 🔵​加入开发者的《专属社群》,分享交流,技术之路不再孤独,一起变强。
  • 🟣点击下方名片获取更多内容🍭🍭🍭👇

http://www.lryc.cn/news/593358.html

相关文章:

  • 月舟科技近调记录
  • 网络爬虫概念初解
  • ndexedDB 与 LocalStorage:全面对比分析
  • C++数据结构————集合
  • 【Keil5-map文件】
  • 阿里云服务器 CentOS 7 安装 MySQL 8.4 超详细指南
  • c#泛型集合(ArrayList和List、Dictionary的对比)
  • 每日面试题09:进程、线程、协程的区别
  • 48Days-Day03 | 删除公共字符,两个链表的第一个公共结点,mari和shiny
  • 【每日算法】专题十五_BFS 解决 FloodFill 算法
  • HD Video Converter Factory pro 高清视频转换器 v27.7.0 绿色中文便携版
  • 【2025最新】 .NET FrameWork微软离线运行库合集,一键安装版
  • Spring之【AnnotatedBeanDefinitionReader】
  • 前端面试专栏-工程化:28.团队协作与版本控制(Git)
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现动物分类(C#源码,UI界面版)
  • Selenium 中 findElement 方法全解析:定位网页元素的 7 种方式
  • RPC(Remote Procedure Call,远程过程调用)介绍
  • 探秘边缘安全架构设计要点解析
  • 深入了解 find_element 方法:Web 自动化定位元素的核心​
  • Node.js特训专栏-实战进阶:17.会话管理与安全存储
  • 开发框架安全ThinkPHPLaravelSpringBootStruts2SpringCloud复现
  • SLAM中的非线性优化-2D图优化之激光SLAM基于优化的前端匹配(十八)
  • KVM中使用桥接模式.运维就业技术教程
  • 零基础学习性能测试-linux服务器监控:CPU监控
  • 【RK3576】【Android14】USB开发调试
  • 《Spring Boot 插件化架构实战:从 SPI 到热插拔的三级跳》
  • Android14 SystemUI 启动流程(2)
  • Verilog *2* SPI-立创逻辑派G1测试-1
  • 软件警告弹窗与兼容性问题
  • 当OT遇见IT:Apache IoTDB如何用“时序空间一体化“破解工业物联网数据孤岛困局