Use DataFrame.rename by Series for set new columns names by another data:
df2 = df1.rename(columns=df.set_index('Question')['ID'])
print (df2)
1 2 3
0 Male Single Doctor
1 Male Divorced Engineer
EDIT:
There are duplicates in Question values in df, so need create unique Question values. One possible solution is remove duplicates by DataFrame.drop_duplicates, here are sample data for see how it working:
print (df)
Question ID
0 gender 10 <-duplicates, change ID for test
1 gender 15 <-duplicates, change ID for test
2 what is your gender 1
3 sexual orientation 1
4 marital status 2
5 occupation 3
6 whats you job 3
You can test what are duplciates in real data:
print (df[df.duplicated('Question', keep=False)])
Question ID
0 gender 10
1 gender 15
Removed duplicates and keep first dupe row, here ID=10:
print (df.drop_duplicates('Question').set_index('Question')['ID'])
Question
gender 10
what is your gender 1
sexual orientation 1
marital status 2
occupation 3
whats you job 3
Name: ID, dtype: int64
df21 = df1.rename(columns=df.drop_duplicates('Question').set_index('Question')['ID'])
print (df21)
10 2 3
0 Male Single Doctor
1 Male Divorced Engineer
Removed duplicates and keep first dupe row, here ID=15:
print (df.drop_duplicates('Question', keep='last').set_index('Question')['ID'])
Question
gender 15
what is your gender 1
sexual orientation 1
marital status 2
occupation 3
whats you job 3
Name: ID, dtype: int64
df22 = df1.rename(columns=df.drop_duplicates('Question', keep='last').set_index('Question')['ID'])
print (df22)
15 2 3
0 Male Single Doctor
1 Male Divorced Engineer
print (df.set_index('Question')['ID'].to_dict())
{'gender': 15, 'what is your gender': 1, 'sexual orientation': 1, 'marital status': 2, 'occupation': 3, 'whats you job': 3}
df22 = df1.rename(columns=df.set_index('Question')['ID'].to_dict())
print (df22)
15 2 3
0 Male Single Doctor
1 Male Divorced Engineer
EDIT1: If values in master DataFrame not exist and is necessary first append them use:
print (df)
Question ID
0 gender 1
1 sex 1
2 what is your gender 1
3 sexual orientation 1
4 marital status 2
5 occupation 3
6 whats you job 3
print (df1)
gender marital status country code1 code2
0 Male Single India 4 7
1 Male Divorced UK 3 5
Get all columns which not exist in df['Question']:
cols = df1.columns.difference(df['Question'].tolist(), sort=False)
print (cols)
Index(['country', 'code1', 'code2'], dtype='object')
Add ID next by maximal value:
df3 = pd.DataFrame({'Question':cols,
'ID': np.arange(df['ID'].max() + 1, len(cols) + df['ID'].max() + 1)})
print (df3)
Question ID
0 country 4
1 code1 5
2 code2 6
Append to original master DataFrame:
df = pd.concat([df, df3], ignore_index=True)
print (df)
Question ID
0 gender 1
1 sex 1
2 what is your gender 1
3 sexual orientation 1
4 marital status 2
5 occupation 3
6 whats you job 3
7 country 4
8 code1 5
9 code2 6
Last use original solution:
df2 = df1.rename(columns=df.set_index('Question')['ID'])
print (df2)
1 2 4 5 6
0 Male Single India 4 7
1 Male Divorced UK 3 5