You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Pandas DataFrame中获取target_label为'Security Alert'的最新3条时间戳记录

如何从Pandas DataFrame中获取target_label为'Security Alert'的最新3条时间戳记录

没问题,我来帮你一步步搞定这个需求!咱们得先把时间列转换成可排序的格式,再筛选目标行、排序取数就行,具体操作如下:

步骤1:把时间列转为datetime类型

你日志里的时间是字符串格式,得先转换成datetime类型才能正确按时间排序。假设你的DataFrame已经命名为df,第二列的列名是timestamp,执行这段代码:

import pandas as pd

# 转换时间列为datetime格式
df['timestamp'] = pd.to_datetime(df['timestamp'])

步骤2:筛选目标标签并取最新3条

接下来筛选出target_label等于'Security Alert'的行,按时间戳降序排列(最新的在前),最后取前3条:

# 筛选+排序+取前3
latest_security_alerts = df[df['target_label'] == 'Security Alert'] \
                          .sort_values(by='timestamp', ascending=False) \
                          .head(3)

对应你提供的数据,最终结果会是这3条:

根据你给出的日志内容,最新的3条安全告警记录是:

  1. 1759,12/29/2025 20:33,ModernCRM,Unauthorised access attempt from 192.168.99.79 detected,Security Alert,bert
  2. 43,11/22/2025 11:06,BillingSystem,"Abnormal system behavior on server 40, potential security breach",Security Alert,bert
  3. 2213,9/1/2025 10:30,AnalyticsEngine,Security alert: suspicious activity from 192.168.214.63,Security Alert,bert

小提醒

如果你的DataFrame列名不是timestamp和target_label,记得替换成实际的列名哦!比如第二列叫log_time、第三列叫alert_type,就把代码里的对应名称换掉就行。

备注:内容来源于stack exchange,提问作者Nilanjan Roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 11:02:59