I have two pandas dataframe structured like so:
DF1:
|'ID'|'Zone'|
|:---------:|
| 11 | 1 |
| 12 | 2 |
| 10 | 0 |
DF2:
|'ID'|'Time'|
|:---------:|
| 11 | 1 |
| 11 | 2 |
| 12 | 1 |
| 12 | 2 |
And I want to add a new column to DF2 named zone, that contain the correct value for which zone each ID belong to. See example below.
|'ID'|'Time'|'Zone'|
|:----------------:|
| 11 | 1 | 1 |
| 11 | 2 | 1 |
| 12 | 1 | 2 |
| 12 | 2 | 2 |
For this small example I have written some code that works fine, but I will like to use is on two large DF. So my qustion is, is there a more delicated (better) way to do this? My current code is:
df2 = np.empty([len(df2.index)]
for i in df2.index:
for j in df.index:
if df2['id'][i] == df1['id'][j]:
df2.loc[i, 'zone'] = df1.loc[j, 'zone']