最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Pandas讀取Excel數(shù)據(jù)并根據(jù)時間字段篩選數(shù)據(jù)

 更新時間:2025年07月22日 10:05:06   作者:岫珩  
這篇文章主要為大家詳細介紹了Python如何調用Pandas實現(xiàn)讀取Excel數(shù)據(jù)并根據(jù)時間字段篩選數(shù)據(jù),感興趣的小伙伴可以跟隨小編一起學習一下

1. 需求描述

現(xiàn)在有一個excel表格,其中包含設備字段device_id、最后使用時間字段end_time以及其他字段若干

需要將表格中的每個設備對應的最新的使用時間篩選出來,并在結果中根據(jù)最新時間篩選出4月和5月

對應的設備號列表

2. 讀取excel表格

import pandas as pd

# 讀取 Excel 文件
file_path = r"C:\Users\Downloads\file_record.xlsx"  # 替換為你的文件路徑
df = pd.read_excel(file_path)
# 顯示前幾行數(shù)據(jù)
# print(df.head())
# print(df)

3. 篩選最新時間

先根據(jù)時間重置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

然后根據(jù)設備號分組,再取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. 篩選具體月份數(shù)據(jù)

在上面的最新時間中篩選出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)

# 顯示前幾行數(shù)據(jù)
# 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數(shù)據(jù)并根據(jù)時間字段篩選數(shù)據(jù)的文章就介紹到這了,更多相關Pandas讀取Excel數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python輕松實現(xiàn)圖片文字提取的高效技巧分享

    Python輕松實現(xiàn)圖片文字提取的高效技巧分享

    隨著數(shù)字化轉型的加速,從圖片中提取文字(OCR,光學字符識別)的需求日益增長,Python憑借其豐富的庫和易用性,成為實現(xiàn)OCR的首選工具之一,本文將深入探討如何利用Python從圖片中提取文字,涵蓋基本原理、常用工具、代碼實現(xiàn)及優(yōu)化技巧,需要的朋友可以參考下
    2025-07-07
  • Python使用FastAPI+FastCRUD自動生成API接口的實現(xiàn)

    Python使用FastAPI+FastCRUD自動生成API接口的實現(xiàn)

    FastCRUD 是一個為 FastAPI + SQLAlchemy 場景設計的開源庫,由 Benav Labs 維護,旨在簡化 CRUD 端點搭建與數(shù)據(jù)庫操作的流程,本文呢給大家介紹了Python使用FastAPI+FastCRUD自動生成API接口的實現(xiàn),需要的朋友可以參考下
    2025-11-11
  • Python使用socket_TCP實現(xiàn)小文件下載功能

    Python使用socket_TCP實現(xiàn)小文件下載功能

    這篇文章主要介紹了Python使用socket_TCP實現(xiàn)小文件下載功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Python Final 類型限定符詳解

    Python Final 類型限定符詳解

    Python的Final是一種類型限定符,用于標記變量、屬性或函數(shù)參數(shù),防止重新賦值,限制類的繼承和方法重寫,提高代碼質量和可維護性,具有一定的參考價值,感興趣的可以了解一下
    2026-05-05
  • Python使用騰訊云API實現(xiàn)短信驗證碼功能

    Python使用騰訊云API實現(xiàn)短信驗證碼功能

    使用Python與騰訊云接口對接,實現(xiàn)短信驗證碼功能變得非常簡單,只需要幾行代碼就能夠輕松實現(xiàn)短信的發(fā)送,無須關心復雜的短信協(xié)議和底層實現(xiàn),讀者可以根據(jù)自己的實際需求,靈活使用騰訊云短信SDK提供的API來實現(xiàn)更豐富的短信功能
    2024-01-01
  • pandas解決數(shù)據(jù)缺失、重復的方法與實踐過程

    pandas解決數(shù)據(jù)缺失、重復的方法與實踐過程

    這篇文章主要介紹了pandas解決數(shù)據(jù)缺失、重復的方法與實踐過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 記一次pyinstaller打包pygame項目為exe的過程(帶圖片)

    記一次pyinstaller打包pygame項目為exe的過程(帶圖片)

    這篇文章主要介紹了記一次pyinstaller打包pygame項目為exe的過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-03-03
  • arcgis使用Python腳本進行批量截圖功能實現(xiàn)

    arcgis使用Python腳本進行批量截圖功能實現(xiàn)

    最近公司數(shù)據(jù)部那邊有個需求,需要結合矢量數(shù)據(jù)和影像數(shù)據(jù),進行批量截圖,并且截圖中只能有一個圖斑,還要添加上相應的水印,這篇文章主要介紹了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

最新評論

惠来县| 铜山县| 南皮县| 南郑县| 修文县| 彭泽县| 固安县| 建阳市| 定日县| 青田县| 乐业县| 日土县| 博白县| 东阿县| 冀州市| 徐闻县| 轮台县| 平昌县| 会昌县| 阳西县| 交口县| 广德县| 巴中市| 连山| 陈巴尔虎旗| 易门县| 嘉义市| 利川市| 武穴市| 拉萨市| 南江县| 自治县| 花莲市| 凤山县| 宜良县| 华阴市| 攀枝花市| 昆山市| 那坡县| 积石山| 上饶市|