import pandas as pd
df1 = pd.DataFrame({'date': ['2015-01-01', '2015-01-10', '2015-01-11', '2015-01-12'], 'a': [1,2,3,4]})
df2 = pd.DataFrame({'date': ['2015-01-01', '2015-01-05', '2015-01-11'], 'b': [10,20,30]})
df = df1.merge(df2, on=['date'], how='outer')
df = df.sort_values('date')
print df
"喜欢磁性物品"可能不是一个很好的表达标题。我将在下面解释。
我希望来自df2的记录与df1的第一条记录相匹配,该记录的日期大于或等于df2' s。例如,我想要df2' 2015-01-05'匹配df1' 2015-01-10'。
我无法通过简单地将它们合并到inner
,outer
,left
方式来实现。虽然,上述结果非常接近我想要的结果。
a date b
0 1.0 2015-01-01 10.0
4 NaN 2015-01-05 20.0
1 2.0 2015-01-10 NaN
2 3.0 2015-01-11 30.0
3 4.0 2015-01-12 NaN
如何从我所做的或从头开始以其他方式实现这一目标?
a date b
0 1.0 2015-01-01 10.0
1 2.0 2015-01-10 20.0
2 3.0 2015-01-11 30.0
3 4.0 2015-01-12 NaN
答案 0 :(得分:1)
确保您的日期为日期
df1.date = pd.to_datetime(df1.date)
df2.date = pd.to_datetime(df2.date)
<强> numpy
强>
np.searchsorted
ilocs = df1.date.values.searchsorted(df2.date.values)
df1.loc[df1.index[ilocs], 'b'] = df2.b.values
df1
a date b
0 1 2015-01-01 10.0
1 2 2015-01-10 20.0
2 3 2015-01-11 30.0
3 4 2015-01-12 NaN
<强> pandas
强>
pd.merge_asof
让你非常接近
pd.merge_asof(df1, df2)
a date b
0 1 2015-01-01 10
1 2 2015-01-10 20
2 3 2015-01-11 30
3 4 2015-01-12 30