0

I have the following 2 data frames:

df1 = pd.DataFrame({
    'identifier': ['1','2','3'],
    'wait times k': [12,13,14]
})

df2 = pd.DataFrame({
'identifier': ['2','4','3','6'],
'bc': [10,33,50,30]

})

I want to add a new column to df2 where it contains 'wait times k' in df1 matched based on the identifiers.

Desired Output:

identifier  bc  wait times k
0   2      10      13
1   4      33      Nan
2   3      50      14
3   6      30      Nan
xuan
  • 59
  • 5
  • 1
    `df2.merge(df1, how='left')` – Psidom Mar 06 '22 at 23:29
  • what if df2 has more than one column? for instance, df2 = pd.DataFrame({ 'identifier': ['2','4','3','6'], 'amount': [100,330,50,30] }) – xuan Mar 06 '22 at 23:36
  • It doesn't matter. It should work the same. – Psidom Mar 06 '22 at 23:37
  • Can I only add df1['wait times k'] instead of the whole dt1? In your code when I replace df1 with df1['wait times k'] it returns this error: No common columns to perform merge on. Merge options: left_on=None, right_on=None, left_index=False, right_index=False – xuan Mar 06 '22 at 23:40
  • 1
    You need to add the `identifier` column as well. at the minimum `df2.merge(df1[['identifier', 'wait times k']], how='left')`. – Psidom Mar 06 '22 at 23:45

0 Answers0