python - 如何根据多个条件将 1 个 pandas 数据帧合并或组合到另一个数据帧
问题描述
我有2个数据框:
df1 和 df2 ,df1 用来作为 df2 的参考或查找文件。这意味着我们需要使用 df1 的每一行来匹配 df2 的每一行,然后将 df1 合并到 df2 中,然后输出新的 df2。
df1:
RB BeginDate EndDate Valindex0
0 00 19000100 19811231 45
1 00 19820100 19841299 47
2 00 19850100 20010699 50
3 00 20010700 99999999 39
df2:
RB IssueDate gs
0 L3 19990201 8
1 00 19820101 G
2 48 19820101 G
3 50 19820101 G
4 50 19820101 G
5 00 19860101 G
6 52 19820101 G
7 53 19820101 G
8 00 19500201 G
如何根据条件合并这两个数据框:
if df1['BeginDate'] <= df2['IssueDate'] <= df1['EndDate'] and df1['RB']==df2['RB']:
merge the value of df1['Valindex0'] to df2
注意最后的输出是将df1合并到df2,因为df1就像是df2的参考或查找文件。这意味着我们需要使用df1的每一行来匹配df2的每一行,然后输出新的df2
输出应如下所示:
df2:
RB IssueDate gs Valindex0
0 L3 19990201 8 None
1 00 19820101 G 47 # df2['RB']==df1['RB'] and df2['IssueDate'] between df1['BeginDate'] and df1['EndDate'] of this row
2 48 19820101 G None
3 50 19820101 G None
4 50 19820101 G None
5 00 19860101 G 50
6 52 19820101 G None
7 53 19820101 G None
8 00 19500201 G 45
我知道一种方法可以做到这一点,但它非常慢,尤其是当 d1 的长度很大时:
conditions = []
for index, row in df1.iterrows():
conditions.append((df2['IssueDate']>= df1['BeginDate']) &
(df2['IssueDate']<= df1['BeginDate'])&
(df2['RB']==df1['RB']))
df2['Valindex0'] = np.select(conditions, df1['Valindex0'], default=None)
有更快的解决方案吗?
解决方案
使用IntervalIndex -
idx = pd.IntervalIndex.from_arrays(df1['BeginDate'],df1['EndDate'],closed='both')
for x in df1['RB'].unique():
mask = df2['RB']==x
df2.loc[mask, 'Valindex0'] = df1.loc[idx.get_indexer(df2.loc[mask, 'IssueDate']), 'Valindex0'].values
输出
RB IssueDate gs Valindex0
0 L3 19990201 8 NaN
1 00 19820101 G 47.0
2 48 19820101 G NaN
3 50 19820101 G NaN
4 50 19820101 G NaN
5 00 19860101 G 50.0
6 52 19820101 G NaN
7 53 19820101 G NaN
8 00 19500201 G 45.0
推荐阅读
- x86 - 汇编 x86(16 位)VSync(屏幕撕裂)
- python - Tkinter - 隐藏提示
- python - 在 TensorFlow BinaryCrossentropy 损失函数中配置标签
- continuous-integration - 如何根据gitlabci上的规则定义全局变量?
- c++ - 如何使用 BGL 计算边缘介数
- django-views - 在 Django 视图中,我可以直接在 View 中使用 GET 获取传递的值,但不能使用 POST
- netlogo - 遍历 Netlogo 中的列表
- ios - iOS swift unit testing - 如何测试调用 api 的函数?
- javascript - 失败,因为:无法找到要上传的文件,但路径正确(Vimeo API)
- python - Python - 带有字母、特殊字符、空格的正则表达式 Sub() 模式