在范围交叉点上合并两个DataFrame

时间:2017-06-05 13:00:46

标签: python pandas

我正在尝试根据min-max值的交集合并两个DataFrame。有没有人有很好的方法与熊猫一起做?

##  min  max  x1       ##  min  max   x2
##0  1    20   0.5     ##0  1    12   1.2
##1  20   30   1.5     ##1  12   30   2.2

期望的输出:

##   min  max  x1   x2
##0  1    12   0.5  1.2
##1  12   20   0.5  2.2
##2  20   30   1.5  2.2

THX!

1 个答案:

答案 0 :(得分:1)

根据您上面的数据集,它可以为您提供所需的内容,但我觉得它可能无法在更复杂的情况下使用。

<强>代码:

# Simple data frame append - since it looks like you want it ordered, you can order it here, and then reset index.
df = df1.append(df2).sort_values(by = 'max')[['min','max','x1','x2']].reset_index(drop = True)

# Here, set 'min' for all but the first row to the 'max' of the previous row
df.loc[1:, 'min'] = df['max'].shift()

# Fill NaNs 
df.fillna(method = 'bfill', inplace = True)

# Filter out rows where min == max
df = df.loc[df['min'] != df['max']]

<强>输出:

    min  max   x1   x2
0   1.0   12  0.5  1.2
1  12.0   20  0.5  2.2
2  20.0   30  1.5  2.2