16

我有两个数据框,第一个是形式(注意日期是日期时间对象):

df = DataFrame('key': [0,1,2,3,4,5],
               'date': [date0,date1, date2, date3, date4, date5],
               'value': [0,10,20,30,40,50])

第二个是以下形式:

df2 = DataFrame('key': [0,1,2,3,4,5],
                'valid_from': [date0, date0, date0, date3, date3, date3],
                'valid_to': [date2, date2, date2, date5, date5, date5],
                'value': [0, 100, 200, 300, 400, 500])

而且我正在尝试有效地加入键匹配的位置并且日期在valid_from和valid_to之间。我想出的是以下内容:

def map_keys(df2, key, date):
    value = df2[df2['key'] == key & 
        df2['valid_from'] <= date & 
        df2['valid_to'] >= date]['value'].values[0]
    return value

keys = df['key'].values
dates = df['date'].values
keys_dates = zip(keys, dates)

values = []
for key_date in keys_dates:
    value = map_keys(df2, key_date[0], key_date[1])
    values.append(value)

df['joined_value'] = values

虽然这似乎可以完成这项工作,但它感觉并不是一个特别优雅的解决方案。我想知道是否有人对这样的加入有更好的主意。

感谢您的帮助 - 非常感谢。

4

1 回答 1

19

目前,您可以使用内置pandas.merge()布尔索引通过几个步骤完成此操作。

merged = df.merge(df2, on='key')

valid = (merged.date >= merged.valid_from) & \
        (merged.date <= merged.valid_to)

df['joined_value'] = merged[valid].value_y

(注意: 的value列在合并后df2访问,value_y因为它与同名的列冲突,df并且默认的合并冲突后缀分别_x, _y用于左帧和右帧。)

这是一个示例,使用不同的设置来显示如何处理无效日期。

n = 8
dates = pd.date_range('1/1/2013', freq='D', periods=n)
df = DataFrame({'key': np.arange(n),
                'date': dates,
                'value': np.arange(n) * 10})
df2 = DataFrame({'key': np.arange(n),
                 'valid_from': dates[[1,1,1,1,5,5,5,5]],
                 'valid_to': dates[[4,4,4,4,6,6,6,6]],
                 'value': np.arange(n) * 100})

输入df2

   key          valid_from            valid_to  value
0    0 2013-01-02 00:00:00 2013-01-05 00:00:00      0
1    1 2013-01-02 00:00:00 2013-01-05 00:00:00    100
2    2 2013-01-02 00:00:00 2013-01-05 00:00:00    200
3    3 2013-01-02 00:00:00 2013-01-05 00:00:00    300
4    4 2013-01-06 00:00:00 2013-01-07 00:00:00    400
5    5 2013-01-06 00:00:00 2013-01-07 00:00:00    500
6    6 2013-01-06 00:00:00 2013-01-07 00:00:00    600
7    7 2013-01-06 00:00:00 2013-01-07 00:00:00    700

中间框架merged

                 date  key  value_x          valid_from            valid_to  value_y
0 2013-01-01 00:00:00    0        0 2013-01-02 00:00:00 2013-01-05 00:00:00        0
1 2013-01-02 00:00:00    1       10 2013-01-02 00:00:00 2013-01-05 00:00:00      100
2 2013-01-03 00:00:00    2       20 2013-01-02 00:00:00 2013-01-05 00:00:00      200
3 2013-01-04 00:00:00    3       30 2013-01-02 00:00:00 2013-01-05 00:00:00      300
4 2013-01-05 00:00:00    4       40 2013-01-06 00:00:00 2013-01-07 00:00:00      400
5 2013-01-06 00:00:00    5       50 2013-01-06 00:00:00 2013-01-07 00:00:00      500
6 2013-01-07 00:00:00    6       60 2013-01-06 00:00:00 2013-01-07 00:00:00      600
7 2013-01-08 00:00:00    7       70 2013-01-06 00:00:00 2013-01-07 00:00:00      700

df添加列后的最终值joined_value

                 date  key  value  joined_value
0 2013-01-01 00:00:00    0      0           NaN
1 2013-01-02 00:00:00    1     10           100
2 2013-01-03 00:00:00    2     20           200
3 2013-01-04 00:00:00    3     30           300
4 2013-01-05 00:00:00    4     40           NaN
5 2013-01-06 00:00:00    5     50           500
6 2013-01-07 00:00:00    6     60           600
7 2013-01-08 00:00:00    7     70           NaN
于 2013-01-13T22:41:47.677 回答