0

df1

ASSET PRICE
BTC 50000
ETH 4000
ADA 3
XRP 1

df2

ASSET AMOUNT
BTC 1
ETH 10
ADA 100
SOL 50
XRP 40
BNB 200

Given these 2 dataframes I would like to add the amount column from df2 to df1 only if the asset is the same. End result for df1 would need to be (only keep assets in df1):

df1

ASSET PRICE AMOUNT
BTC 50000 1
ETH 4000 10
ADA 3 100
XRP 1 40
Diego M
  • 1
  • 2
  • This operation is called [`merge`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html) -> `df1 = df1.merge(df2, on='ASSET', how='left')` (See [left merge](https://stackoverflow.com/a/53645883/15497888)) – Henry Ecker Sep 03 '21 at 23:49
  • Or with [`Series.map`](https://pandas.pydata.org/docs/reference/api/pandas.Series.map.html) -> `df1['AMOUNT'] = df1['ASSET'].map(df2.set_index('ASSET')['AMOUNT'])` (See [This answer](https://stackoverflow.com/a/52703621/15497888)) – Henry Ecker Sep 03 '21 at 23:51

0 Answers0