Let's say I originally have a dataframe df
:
name | age | |
---|---|---|
0 | Robert | 21 |
1 | Bob | 22 |
2 | Mike | 23 |
And there's also a dataframe df2
:
name | attribute | value | |
---|---|---|---|
0 | Robert | robert@email.com | |
1 | Bob | bob@email.com | |
2 | Robert | married? | Yes |
3 | Mike | married? | No |
4 | Mike | employed? | Yes |
5 | Robert | have a car? | Yes |
6 | Bob | have a car? | No |
Beforehand, I already know that a person will have the attributes, for example, "age", "email", "married?", "employed?", "have a car?", "imigrant?", "student?".
How can I get a df_final
like this:
name | age | married? | employed? | have a car? | imigrant? | student? | ||
---|---|---|---|---|---|---|---|---|
0 | Robert | 21 | robert@email.com | Yes | Yes | |||
1 | Bob | 22 | bob@email.com | No | ||||
2 | Mike | 23 | No | Yes |
I thought about creating the attributes columns by hand in df
and then use df.loc[df['name']==df2['name'], df2['attribute']]
to find find the correct indexes to insert the values, but I couldn't get it right.