Python Pandas讀取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使用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)小文件下載功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
pandas解決數(shù)據(jù)缺失、重復的方法與實踐過程
這篇文章主要介紹了pandas解決數(shù)據(jù)缺失、重復的方法與實踐過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
記一次pyinstaller打包pygame項目為exe的過程(帶圖片)
這篇文章主要介紹了記一次pyinstaller打包pygame項目為exe的過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
arcgis使用Python腳本進行批量截圖功能實現(xiàn)
最近公司數(shù)據(jù)部那邊有個需求,需要結合矢量數(shù)據(jù)和影像數(shù)據(jù),進行批量截圖,并且截圖中只能有一個圖斑,還要添加上相應的水印,這篇文章主要介紹了arcgis使用Python腳本進行批量截圖,需要的朋友可以參考下2023-01-01

