I want to create a column with data from another dataframe based on index.
For example, I have a dataframe below:
import numpy as np
import pandas as pd
df1={'id':[1,1,1,3,5,6,7,8,9,10], 'name':['a','a','a','c','e','f','g','h','i','j']}
df2 = {'id':[1,2,3,4,5,6,7,8,9,10], 'age':[21,11,45,11,56,22,26,26,17,32], 'gender':['M','M','f','f','M','f','M','M','f','M']}
df1 = pd.DataFrame(df1)
df1.set_index('id', inplace = True)
df2 = pd.DataFrame(df2)
df2.set_index('id', inplace = True)
Using this two dataframe, I want to create a column in df1 called 'gender' and get the data from df2 based on its index. So my column 'gender' in df1 will look like this:
['M','M','M','f','M','f','M','M','f','M']