在重叠的段上合并两个熊猫数据框

时间:2020-02-20 23:23:18

标签: python pandas dataframe merge

我有两个熊猫数据框,并且如果两列(具有startend坐标)重叠且没有边界,则需要匹配行。

例如:

df_1 = pd.DataFrame(data={'start': [0, 10, 23, 35], 'end': [5, 17, 28, 41], 'some_data_1': ['AA', 'BB', 'CC', 'DD']})
df_2 = pd.DataFrame(data={'start': [0, 12, 23, 55], 'end': [5, 17, 25, 62], 'some_data_2': ['AA_AA', 'BB_BB', 'CC_CC', 'DD_DD']})

哪里

df_1 :
    start   end some_data_1
        0     5          AA
       10    17          BB
       23    28          CC
       35    41          DD

df_2 :
    start   end some_data_2
        0     5       AA_AA
       12    17       BB_BB
       23    25       CC_CC
       55    62       DD_DD

,所需的输出是:

df_1_2 :
    start_1 end_1   start_2 end_2  some_data_1  some_data_2
          0     5         0     5           AA        AA_AA
         10    17        12    17           BB        BB_BB
         23    28        23    25           CC        CC_CC
         35    41       NaN   NaN           DD          NaN
        NaN   NaN        55    62          NaN        DD_DD       

是否有一种优雅的方法来检查一个段(由end-start给定)是否与另一个段重叠,如果重叠,则在这种情况下合并数据帧。

谢谢!

1 个答案:

答案 0 :(得分:2)

创建一个条件以查找两个框架之间是否存在重叠,根据条件创建新列,然后使用how ='outer'

进行合并

我从数据中观察到的是,如果df_1中的重叠(结束-开始)大于或等于df_2中的重叠,则添加start_data_2,否则保持原样。计算取决于此;如果这是错误的前提OP,请告诉我。

#create overlap columns

df_1['overlap']= df_1.end - df_1.start
df_2['overlap']= df_2.end - df_2.start

cond1 = df_1.overlap.ge(df_2.overlap)
df_1['key'] = np.where(cond1, df_2.some_data_2,'n1')
df_2['key'] = np.where(cond1, df_2.some_data_2,'n')

(pd
 .merge(df_1,df_2,
        how='outer',
        on='key',
        suffixes = ('_1','_2'))
 .drop(['key','overlap_1','overlap_2'],
       axis=1)
  )

   start_1  end_1   some_data_1 start_2 end_2   some_data_2
0   0.0     5.0        AA        0.0    5.0      AA_AA
1   10.0    17.0       BB       12.0    17.0     BB_BB
2   23.0    28.0       CC       23.0    25.0     CC_CC
3   35.0    41.0       DD       NaN     NaN      NaN
4   NaN     NaN        NaN      55.0    62.0     DD_DD