0

I have two dataframes with the following data

data1 = [['date1', 10], ['date2', 15], ['date3', 14]]
df1 = pd.DataFrame(data1, columns = ['date', 'value'])

data2 = [['date1', 1], ['date2', 2], ['date3', 3], ['date1', 4], ['date2', 5], ['date3', 6]]
df2 = pd.DataFrame(data2, columns = ['date', 'value'])

example 1

Basically I want it to look like this

data3 = [['date1', 1, 10], ['date2', 2, 15], ['date3', 3, 14], ['date1', 4, 10], ['date2', 5, 15], ['date3', 6, 14]]
df3 = pd.DataFrame(data3, columns = ['date', 'value', 'other value'])
df3

enter image description here

Where the values from the first dataframe are mapped to specific row values. Is this possible in Pandas? I haven't been able to find out how to do this and I am starting to believe this is not a possibility.

Evan Kim
  • 769
  • 2
  • 8
  • 26

1 Answers1

2

You can use assign() method and set_index() method:

df3=df2.set_index('date').assign(other_value=df1.set_index('date'))

OR

As suggested by @anky in comment use merge() method as it is easier to write:

df3=df2.merge(df1,on='date',how='left')

Performace:

enter image description here

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41