python - Pandas - 按时间接近加入

标签 python join pandas timestamp dataframe

我有 2 个数据帧,left_dfright_df,每个数据帧都有一个对应于日期时间的列。我想以这样的方式加入它们,对于 left_df 中的每一行 R,我都在 right_df 中找到时间最接近的行right_df 中的所有行中的 R,并将它们放在一起。我不知道来自 left_df 还是来自 right_df 的行是第一个。

例子如下:

left_df = 
              left_dt           left_flag
0  2014-08-23 07:57:03.827516   True
1  2014-08-23 09:27:12.831126  False
2  2014-08-23 11:55:27.551029   True
3  2014-08-23 16:11:33.511049   True


right_df =
    right dt                   right_flag 
0   2014-08-23 07:12:52.80587    True
1   2014-08-23 15:12:34.815087   True




desired output_df =

              left_dt           left_flag        right dt               right_flag 
0  2014-08-23 07:57:03.827516   True        2015-08-23 07:12:52.80587      True
1  2014-08-23 09:27:12.831126  False        2015-08-23 07:12:52.80587      True
2  2014-08-23 11:55:27.551029   True        2015-08-23 15:12:34.815087     True
3  2014-08-23 16:11:33.511049   True        2015-08-23 15:12:34.815087     True

最佳答案

我不确定它是否适用于所有情况。但我认为这可能是一个解决方案。

# Test data
left_df = pd.DataFrame({'left_dt': ['2014-08-23 07:57:03.827516',
  '2014-08-23 09:27:12.831126',
  '2014-08-23 11:55:27.551029',
  '2014-08-23 16:11:33.511049'],
 'left_flag': [True, False, True, True]})
left_df['left_dt'] = pd.to_datetime(left_df['left_dt'])


right_df = pd.DataFrame(
{'right_dt': ['2014-08-23 07:12:52.80587', '2014-08-23 15:12:34.815087'],
 'right_flag': [True, True]})
right_df['right_dt'] = pd.to_datetime(right_df['right_dt'])


# Setting the date as the index for each DataFrame
left_df.set_index('left_dt', drop=False, inplace=True)
right_df.set_index('right_dt', drop=False, inplace=True)

# Merging them and filling the gaps
output_df = left_df.join(right_df, how='outer').sort_index()
output_df.fillna(method='ffill', inplace=True)
# Droping unwanted values from the left
output_df.dropna(subset=['left_dt'], inplace=True)
# Computing a difference to select the right duplicated row to drop (the one with the greates diff)
output_df['diff'] = abs(output_df['left_dt'] - output_df['right_dt'])
output_df.sort(columns='diff', inplace=True)
output_df.drop_duplicates(subset=['left_dt'], inplace=True)
# Bringing back the index
output_df.sort_index(inplace=True)
output_df = output_df.reset_index(drop=True)
# Droping unwanted column
output_df.drop('diff', axis=1, inplace=True)
output_df

                     left_dt left_flag                   right_dt right_flag
0 2014-08-23 07:57:03.827516      True 2014-08-23 07:12:52.805870       True
1 2014-08-23 09:27:12.831126     False 2014-08-23 07:12:52.805870       True
2 2014-08-23 11:55:27.551029      True 2014-08-23 15:12:34.815087       True
3 2014-08-23 16:11:33.511049      True 2014-08-23 15:12:34.815087       True

关于python - Pandas - 按时间接近加入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768325/

相关文章:

python - 更正 mt.exe 语法以解决 pyodbc 导入问题

cakephp - 从 CakePHP 中的连接表获取数据

mysql - 在另一个查询中使用一个查询的结果

python - 如何将另一个系列传递给 pandas.Series.str.startswith

python - 二维 numpy 数组中的阈值

python - 解析 XML 模板标签的正则表达式

Python 检测并正确处理多处理管理器关闭

MySQL Select all distinct ID where all rows match the where close

python - 在 pandas.merge_asof 之后保留两个合并键

python - 所有 Pandas 细胞的词形还原