【问题标题】:Merge two pandas dataframe based on conditional根据条件合并两个熊猫数据框
【发布时间】:2021-06-17 14:32:33
【问题描述】:

如果满足预定条件,目标是按行组合两个df。 具体来说,如果列之间的差异小于或等于threshold,则加入df的行。

给定两个df:df1 和 df2,下面的代码部分实现了目标。

import pandas as pd

df1 = pd.DataFrame ( {'time': [2, 3, 4, 24, 31]} )
df2 = pd.DataFrame (  {'time': [4.1, 24.7, 31.4, 5]} )
th = 0.9
all_comb=[]
for index, row in df1.iterrows ():
    for index2, row2 in df2.iterrows ():
        diff = abs ( row ['time'] - row2 ['time'] )
        if diff <= th:
            all_comb.append({'idx_1':index,'time_1':row ['time'], 'idx_2':index2,'time_2':row2 ['time']})
df_all = pd.DataFrame(all_comb)

输出

       idx_1  time_1  idx_2  time_2
0      2       4      0     4.1
1      3      24      1    24.7
2      4      31      2    31.4

但是,上述方法忽略了某些信息,即来自df1 的值 2 和 3,以及来自df2 的值 5。

预期的输出应该是这样的

idx_1  time_1  idx_2  time_2

0      2       NA    NA
1      3       NA    NA    
2       4      0     4.1
3      24      1    24.7
4      31      2    31.4
NA     NA      3     5

感谢任何提示或任何比上述建议更紧凑和高效的方式。

【问题讨论】:

    标签: python pandas join


    【解决方案1】:

    您可以执行交叉合并,然后根据您的条件一次对所有行进行子集化。然后我们concat,从两个 DataFrame 中添加回任何不满足条件的行。

    import pandas as pd
    
    df1 = df1.reset_index().add_suffix('_1')
    df2 = df2.reset_index().add_suffix('_2')
    
    m = df1.merge(df2, how='cross')
    
    # Subset to all matches: |time_diff| <= thresh
    th = 0.9
    m = m[(m['time_1'] - m['time_2']).abs().le(th)]
    
    # Add back rows with no matches
    res = pd.concat([df1[~df1.index_1.isin(m.index_1)],
                     m,
                     df2[~df2.index_2.isin(m.index_2)]], ignore_index=True)
    

    print(res)
       index_1  time_1  index_2  time_2
    0      0.0     2.0      NaN     NaN
    1      1.0     3.0      NaN     NaN
    2      2.0     4.0      0.0     4.1
    3      3.0    24.0      1.0    24.7
    4      4.0    31.0      2.0    31.4
    5      NaN     NaN      3.0     5.0
    

    【讨论】:

    • 感谢cross,没想到
    • 如果您的 DataFrame 很大,它可能会占用大量内存,但无论如何,所有行的双循环将永远持续下去。对于交叉将是
    猜你喜欢
    • 2020-04-24
    • 2018-09-16
    • 2017-12-27
    • 2017-06-11
    • 2016-01-01
    • 1970-01-01
    • 2015-12-28
    相关资源
    最近更新 更多