I am trying to put "inhibitory" or "excitatory" in both columns "postsyn_type" and "presyn_type" of df1, every time that a value in "post_pt_root_id" and "pre_pt_root_id" columns (respectively), matches the value in df2 in the column "pt_root_id".
Examples of dataframes I have:
df1 = pd.DataFrame({'pre_pt_root_id': [1,1,1,2,2], 'post_pt_root_id': [5,1,5,6,7]})
pre_pt_root_id post_pt_root_id
0 1 5
1 1 1
2 1 5
3 2 6
4 2 7
df2 = pd.DataFrame({'pt_root_id': [1,2,3,4,5,6,7], 'type': ['inhib','excit','inhib','inhib','excit','excit','inhib']})
pt_root_id type
0 1 inhib
1 2 excit
2 3 inhib
3 4 inhib
4 5 excit
5 6 excit
6 7 inhib
Example of result:
df1 = pd.DataFrame({'pre_pt_root_id': [1,1,1,2,2], 'post_pt_root_id': [5,1,5,6,7], 'presyn_type': ['inhib','inhib','inhib','excit','excit'], 'postsyn_type': ['excit','inhib','excit','excit','inhib']})
pre_pt_root_id post_pt_root_id presyn_type postsyn_type
0 1 5 inhib excit
1 1 1 inhib inhib
2 1 5 inhib excit
3 2 6 excit excit
4 2 7 excit inhib
I have already tried with merge but it doesn't seem to work well with what I want to do. As you might have noticed, in df1 a value in the column "pre_pt_root_id" can be repeated several times, thus the value i have to put in "presyn_type" must be the same for every repetition. Can anyone help?