Pandas 如何使用布尔索引从第二个数据帧列更新列?

Pandas 如何使用布尔索引从第二个数据帧列更新列?,pandas,dataframe,merge,boolean,Pandas,Dataframe,Merge,Boolean,我有两个数据帧,每个数据帧有两列:unique_id,price。 df1具有df2中所有唯一_id的子集 现在我需要向df1添加第三列,该列包含df2中唯一的_id元素的价格。i、 e.列将为: 唯一的标识、价格、价格2 我该怎么做 考虑数据帧df1和df2 df1 = pd.DataFrame({ 'unique_id': [1, 2, 3], 'price': [11, 12, 13], }) df2 = pd.DataFrame({ 'u

我有两个数据帧,每个数据帧有两列:unique_id,price。 df1具有df2中所有唯一_id的子集

现在我需要向df1添加第三列,该列包含df2中唯一的_id元素的价格。i、 e.列将为: 唯一的标识、价格、价格2


我该怎么做

考虑数据帧
df1
df2

df1 = pd.DataFrame({
        'unique_id': [1, 2, 3],
        'price': [11, 12, 13],
    })

df2 = pd.DataFrame({
    'unique_id': [1, 2, 3, 4, 5],
    'price': [9, 10, 11, 12, 13],
})
合并

df1.merge(df2, on='unique_id', suffixes=['', '2'], how='left')

   price  unique_id  price2
0     11          1       9
1     12          2      10
2     13          3      11
df1.join(df2.set_index('unique_id'), on='unique_id', rsuffix='2')

   price  unique_id  price2
0     11          1       9
1     12          2      10
2     13          3      11
加入

df1.merge(df2, on='unique_id', suffixes=['', '2'], how='left')

   price  unique_id  price2
0     11          1       9
1     12          2      10
2     13          3      11
df1.join(df2.set_index('unique_id'), on='unique_id', rsuffix='2')

   price  unique_id  price2
0     11          1       9
1     12          2      10
2     13          3      11
实验性:快速
使用
numpy.searchsorted


定时
pir1
最快方法

小数据

大数据量
使用@jezrael的测试数据

另一个解决方案:

df1['price_df2'] = df1['unique_id'].map(df2.set_index('unique_id')['price'])
再次借用@piRSquared的示例DFs;-)

使用速度更快:


df1['price_df2'] = df1['unique_id'].map(df2.set_index('unique_id')['price'])
In [42]: df1
Out[42]:
   price  unique_id
0     11          1
1     12          2
2     13          3

In [43]: df2
Out[43]:
   price  unique_id
0      9          1
1     10          2
2     11          3
3     12          4
4     13          5

In [44]: df1['price_df2'] = df1['unique_id'].map(df2.set_index('unique_id')['price'])

In [45]: df1
Out[45]:
   price  unique_id  price_df2
0     11          1          9
1     12          2         10
2     13          3         11
df1 = pd.DataFrame({'unique_id':[1,2,3,1,2,3],
                   'price':[4,5,6,7,8,9]})

print (df1)

df2 = pd.DataFrame({'unique_id':[1,2,3],
                    'price':[46,55,44]})

print (df2)

df1['price2'] = df1['unique_id'].map(df2.set_index('unique_id')['price'])
print (df1)
   price  unique_id  price2
0      4          1      46
1      5          2      55
2      6          3      44
3      7          1      46
4      8          2      55
5      9          3      44
np.random.seed(123)
N = 1000000
L = np.random.randint(1000,size=N)
df1 = pd.DataFrame({'unique_id': np.random.choice(L, N),
                   'price':np.random.choice(L, N)})
print (df1)

df2 = pd.DataFrame({'unique_id': np.arange(N),
                   'price':np.random.choice(L, N)})

print (df2)

In [60]: %timeit df1['price2'] = df1['unique_id'].map(df2.set_index('unique_id')['price'])
1 loop, best of 3: 168 ms per loop

In [61]: %timeit df1.merge(df2, on='unique_id', suffixes=['', '2'], how='left')
1 loop, best of 3: 373 ms per loop

In [62]: %timeit df1.join(df2.set_index('unique_id'), on='unique_id', rsuffix='2')
1 loop, best of 3: 252 ms per loop