python - 根据参数获得列中的最小时间差
问题描述
这是我的可重复数据-
raw_data = {'file': [123, 342, 223, 134, 235,233],
'identity': [12, 12, 12, 12,14,14],
'line': [1, 2, 3, 4, 5,6],
'date': ['10/27/2013','10/27/2013', '10/27/2013', '10/27/2013', '10/20/2013','10/20/2013'],
'time': ['13:20:00', '13:20:30', '13:21:00', '13:21:30', '15:40:00','15:40:30']}
现在对于给定的参数说'identity'=12 ,'date'=10/27/2013 and 'time'=13:20:21
我现在想创建一个新的数据帧,根据参数标识,日期从数据帧中选择与时间参数具有最小时间差的行。
例如对于参数'identity'=12 ,'date'=10/27/2013 and 'time'=13:20:21
我们有答案-
identity date time difference
12 10/27/2013 13:20:30 9
解决方案
这不是您的代码的样子,因为您没有向我们提供您的尝试。但这应该让您清楚地了解如何解决它
from datetime import datetime
df = pd.DataFrame(raw_data)
cond = (df['identity'] == 12)
cond2 = df['date'] == '10/27/2013'
td = datetime.strptime('13:20:21', '%H:%M:%S')
# series of time differnces
min_time_diff = abs(df.loc[cond & cond2]['time'].apply(lambda x: datetime.strptime(x, '%H:%M:%S') - td))
# return the row with the minimum time difference
out = df.loc[min_time_diff.idxmin()]
out['differce'] = min_time_diff[min_time_diff.idxmin()].components.seconds
出去:
date 10/27/2013
file 342
identity 12
line 2
time 13:20:30
differce 9
Name: 1, dtype: object
推荐阅读
- react-native - 将屏幕从 FlatList 的一个项目更改为该项目的问题屏幕
- javascript - 对使用 Google API Fetch 感到困惑
- c++ - 在多线程中使用延迟函数时如何避免 Qt 中的线程阻塞?
- java - 微服务之微服务中的分页
- c# - 使用反射将属性动态转换为其实际类型(其中实际类型是通用的)
- spring - 使用反应器上下文从下游链获取结果
- python - Django如何将复选框值传递给函数
- vagrant - 如何从一个 VM 获取 env var 并将其传递给 Vagrantfile 中的另一个 VM?
- python - 如何使用 Scrapy 抓取所有页面
- python - 为什么我的代码只打印 x 和 y 一次?