【问题标题】:How to lookup a specific value in a range of one DataFrame an put in in another如何在一个 DataFrame 的范围内查找特定值并放入另一个 DataFrame
【发布时间】:2022-11-23 22:58:42
【问题描述】:

df1:

**Tarif    von    bis   GK**
FedEx    0.0    1.0  G001
FedEx    1.0    2.0  G002
...
DHL.     0.0    0.5  G001
DHL.     0.5    1.0  G002
...
DPD      0.0    5.0  G001
DPD      5.0    10.0 G002

df2:

**Tarif   Weight  GK**

FedEx     0.6
DHL       0.6
FedEx     0.5
DPD       7.5

我的尝试:

for i in range(len(df2)):
      
        df2.loc[[i]['GK'] = df1['GK'].loc[(df1['Tarif'] == df2.loc[[i]]['Tarif'])
                                & (df1['von'] <  df2[[i]]['Weight']) 
                                & (df1['bis'] >= df2[[i]]['Weight'])]
ValueError: Can only compare identically-labeled Series objects*

结果应该是

df2:

**Tarif   Weight  GK****

FedEx     0.6.    G001
DHL       0.6.    G002
FedEx     0.5.    G001
DPD       3.5.    G002

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    使用merge_asof

    (pd.merge_asof(df2.reset_index().drop(columns='GK', errors='ignore')
                      .sort_values(by='Weight'),
                   df1.sort_values(by='von'),
                   left_on='Weight', right_on='von', by='Tarif'
                  )
       .set_index('index')
       # the line below is only necessary if the bins are disjoint
       # or if there is a risk that the Weight is greater than the max "bis"
       .assign(GK=lambda d: d['GK'].mask(d['Weight'].gt(d['bis'])))
       .sort_index()
       #.drop(columns=['von', 'bis']) # uncomment to remove von/bis
    )
    

    输出:

           Tarif  Weight  von   bis    GK
    index                                
    0      FedEx     0.6  0.0   1.0  G001
    1        DHL     0.6  0.5   1.0  G002
    2      FedEx     0.5  0.0   1.0  G001
    3        DPD     7.5  5.0  10.0  G002
    

    【讨论】:

      【解决方案2】:

      另一种可能的解决方案,它基于以下想法:

      1. 像往常一样用pandas.DataFrame.merge 合并两个数据帧。

      2. 过滤掉不满足条件的情况。

        out = df2.iloc[:,:2].merge(df1, on='Tarif')
        out = out.loc[out['von'].lt(out['Weight']) & out['bis'].ge(out['Weight'])]
        out = out.reset_index(drop=True)
        

        输出:

           Tarif  Weight  von   bis    GK
        0  FedEx     0.6  0.0   1.0  G001
        1  FedEx     0.5  0.0   1.0  G001
        2    DHL     0.6  0.5   1.0  G002
        3    DPD     7.5  5.0  10.0  G002
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 1970-01-01
        • 2021-01-17
        • 1970-01-01
        • 2020-03-09
        相关资源
        最近更新 更多