欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1. 需求描述

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

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

對應(yīng)的設(shè)備號列表

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ù)設(shè)備號分組,再取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月的設(shè)備列表

# 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.輸出結(jié)果

遍歷結(jié)果中設(shè)備和時間信息

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

到此這篇關(guān)于Python Pandas讀取Excel數(shù)據(jù)并根據(jù)時間字段篩選數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Pandas讀取Excel數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • postman傳遞當(dāng)前時間戳實例詳解

    postman傳遞當(dāng)前時間戳實例詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于postman傳遞當(dāng)前時間戳知識點相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-09-09
  • 讓你的Python代碼實現(xiàn)類型提示功能

    讓你的Python代碼實現(xiàn)類型提示功能

    今天小編就為大家分享一篇讓你的Python代碼實現(xiàn)類型提示功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python多版本管理與pip升級的全面指南

    Python多版本管理與pip升級的全面指南

    在Python開發(fā)過程中,多版本共存、pip升級失敗和環(huán)境變量沖突是常見問題,本文介紹了如何管理Python多版本和正確升級pip,感興趣的小伙伴可以了解下
    2025-05-05
  • 詳談python中subprocess shell=False與shell=True的區(qū)別

    詳談python中subprocess shell=False與shell=True的區(qū)別

    這篇文章主要介紹了詳談python中subprocess shell=False與shell=True的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • python操作注冊表的方法實現(xiàn)

    python操作注冊表的方法實現(xiàn)

    Python提供了winreg模塊,可以用于操作Windows注冊表,本文就來介紹一下python操作注冊表的方法實現(xiàn),主要包括打開注冊表、讀取注冊表值、寫入注冊表值和關(guān)閉注冊表,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • 深入理解python中的select模塊

    深入理解python中的select模塊

    這篇文章主要介紹了python中select模塊的相關(guān)資料,Python中的select模塊專注于I/O多路復(fù)用,提供了select、poll和epoll三個方法,文中還詳細(xì)的介紹了select和poll,因為poll與select相差不大,所以本文不作介紹,需要的朋友們下面來一起看看吧。
    2017-04-04
  • 使用Python打造專業(yè)演示文稿轉(zhuǎn)換器(Markdown轉(zhuǎn)PPT)

    使用Python打造專業(yè)演示文稿轉(zhuǎn)換器(Markdown轉(zhuǎn)PPT)

    在日常工作中,我們經(jīng)常需要將Markdown格式的文檔轉(zhuǎn)換為演示文稿,手動復(fù)制粘貼不僅繁瑣,還容易出錯,今天我們就來看看如何用Python開發(fā)一個功能完整的Markdown到PPTX轉(zhuǎn)換器
    2025-07-07
  • Python八大常見排序算法定義、實現(xiàn)及時間消耗效率分析

    Python八大常見排序算法定義、實現(xiàn)及時間消耗效率分析

    這篇文章主要介紹了Python八大常見排序算法定義、實現(xiàn)及時間消耗效率分析,結(jié)合具體實例形式對比分析了冒泡排序、直接插入排序、選擇排序、歸并排序、希爾排序、桶排序、堆排序等排序算法的使用與執(zhí)行效率,需要的朋友可以參考下
    2018-04-04
  • Python pip 常用命令匯總

    Python pip 常用命令匯總

    這篇文章主要介紹了Python pip 常用命令匯總,幫助大家更好的理解和使用pip命令,感興趣的朋友可以了解下
    2020-10-10
  • Python從入門到精通之Hash函數(shù)的使用詳解

    Python從入門到精通之Hash函數(shù)的使用詳解

    Python提供了強(qiáng)大而靈活的Hash函數(shù),用于在各種應(yīng)用中實現(xiàn)數(shù)據(jù)存儲、數(shù)據(jù)校驗、加密等功能,下面將從入門到精通介紹Python中Hash函數(shù)的使用,感興趣的可以了解一下
    2023-08-08

最新評論