pandas -查找两个DataFrame之间没有循环的最近日期 [英] Pandas - find nearest dates between two DataFrames without loop

查看:71
本文介绍了 pandas -查找两个DataFrame之间没有循环的最近日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个单独的DataFrame查找最近的上一个日期.实际上,我已经有了执行此操作的代码,但是它使用了for循环,我不想使用它,尤其是因为我的实际DataFrames会比以下代码段大得多:

I'm trying to find the nearest previous date using two separate DataFrames. I've actually got the code to do it, but it uses a for loop, which I would rather not use, especially as my actual DataFrames will be considerably larger than the following snippet:

date_x = pd.to_datetime(['1/15/2015','2/14/2015','3/16/2015','4/15/2015','5/15/2015','6/14/2015','7/14/2015'])
date_y = pd.to_datetime(['1/1/2015','3/1/2015','6/14/2015','8/1/2015'])

dfx = pd.DataFrame({'date_x':date_x})
dfy = pd.DataFrame({'date_y':date_y})

z_list = []
for x in range(dfx['date_x'].count()):
    z_list.append(dfy['date_y'][dfy['date_y'] <= dfx['date_x'][x]].max())

dfx['date_z'] = z_list

产量...

      date_x     date_z
0 2015-01-15 2015-01-01
1 2015-02-14 2015-01-01
2 2015-03-16 2015-03-01
3 2015-04-15 2015-03-01
4 2015-05-15 2015-03-01
5 2015-06-14 2015-06-14
6 2015-07-14 2015-06-14

这正是我想要的,但是再次,我认为还有一种更为泛滥的方式.

which is exactly what I want, but again, I think there is a more pandonic way.

推荐答案

尝试使用 merge_asof()方法:

注意:此方法已在Pandas v.0.19.0中添加

NOTE: this method has been added in Pandas v.0.19.0

In [17]: pd.merge_asof(dfx, dfy, left_on='date_x', right_on='date_y')
Out[17]:
      date_x     date_y
0 2015-01-15 2015-01-01
1 2015-02-14 2015-01-01
2 2015-03-16 2015-03-01
3 2015-04-15 2015-03-01
4 2015-05-15 2015-03-01
5 2015-06-14 2015-06-14
6 2015-07-14 2015-06-14

这篇关于 pandas -查找两个DataFrame之间没有循环的最近日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆