I am trying to create a parent child relationship between two dataframes:
df_5:
parent Tag_Name
0 NaN Mkt30
1 NaN Mkt31
2 NaN Mkt36
3 NaN Mkt37
df_4:
Tag_Name parent
0 Mkt34 1fabfd31-f6aa-4061-a692-35bf6d19c9ae
1 Mkt29 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f
2 Mkt35 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f
3 Mkt30 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f
4 Mkt32 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f
5 Mkt31 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f
6 Mkt36 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f
7 Mkt38 1fabfd31-f6aa-4061-a692-35bf6d19c9ae
8 Mkt39 1fabfd31-f6aa-4061-a692-35bf6d19c9ae
9 Mkt33 1fabfd31-f6aa-4061-a692-35bf6d19c9ae
10 Mkt40 1fabfd31-f6aa-4061-a692-35bf6d19c9ae
11 Mkt37 1fabfd31-f6aa-4061-a692-35bf6d19c9ae
Following is the code that i am using:
df_5.loc[df_5.Tag_Name.isin(df_4.Tag_Name), ['parent']] = df_4[['parent']]
This is supposed to be matching the values from Tag_Name column of df_5, finding it in df_4 and then returning its parent value in df_5 from df_4. This line of code fails when there are more number of rows as shown in the example above.
Here the current output will be as follows which is NOT correct.
df_5:
parent Tag_Name
0 1fabfd31-f6aa-4061-a692-35bf6d19c9ae Mkt30
1 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f Mkt31
2 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f Mkt36
3 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f Mkt37
Ideal expected output should be:
df_5:
parent Tag_Name
0 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f Mkt30
1 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f Mkt31
2 edfff48b-a9d6-4c56-84c3-0fb8dab9b88f Mkt36
3 1fabfd31-f6aa-4061-a692-35bf6d19c9ae Mkt37
Is there any one liner code that will work in this case? I am looking to simplify the solution without using pd.merge which will give me the correct value.