python-按间隔合并两个熊猫数据帧
作者:互联网
我有两个熊猫数据框,格式如下:
df_ts = pd.DataFrame([
[10, 20, 1, 'id1'],
[11, 22, 5, 'id1'],
[20, 54, 5, 'id2'],
[22, 53, 7, 'id2'],
[15, 24, 8, 'id1'],
[16, 25, 10, 'id1']
], columns = ['x', 'y', 'ts', 'id'])
df_statechange = pd.DataFrame([
['id1', 2, 'ok'],
['id2', 4, 'not ok'],
['id1', 9, 'not ok']
], columns = ['id', 'ts', 'state'])
我正在尝试将其转换为格式,例如:
df_out = pd.DataFrame([
[10, 20, 1, 'id1', None ],
[11, 22, 5, 'id1', 'ok' ],
[20, 54, 5, 'id2', 'not ok'],
[22, 53, 7, 'id2', 'not ok'],
[15, 24, 8, 'id1', 'ok' ],
[16, 25, 10, 'id1', 'not ok']
], columns = ['x', 'y', 'ts', 'id', 'state'])
我了解如何通过按id分组然后遍历每一行并在其出现时更改状态来迭代地完成此操作.有没有熊猫内置的更可扩展的方式来做到这一点?
解决方法:
不幸的是,大熊猫合并仅支持平等加入.在以下线程中查看更多详细信息:
merge pandas dataframes where one value is between two others
如果要按间隔合并,则需要解决此问题,例如,在合并后添加另一个过滤器:
joined = a.merge(b,on='id')
joined = joined[joined.ts.between(joined.ts1,joined.ts2)]
标签:pandas,time-series,python,merge 来源: https://codeday.me/bug/20191111/2019245.html