I am trying to create all possible combinations of Positions and Employees by doing a cross join. But I want to add a condition while joining them. For eg. see below sample dataframes
>>> df_pos
Country Pos_id level
0 India 1 2
1 UK 2 2
2 USA 3 4
3 UAE 4 5
4 India 5 1
5 UK 6 3
>>> df_emp
Country Emp_id level
0 UK 11 3
1 UK 12 4
2 USA 13 4
3 Singapore 14 5
4 India 15 2
I want to find combination of Pos_id
and Emp_id
where the country is matched first and then the matching job level should be equal to or +1/-1
, For eg. we have a position in UK
(pos_id 6
) for which we have a match of Employees (11
& 12
) whose country is also UK
and job level
is 3
& 4
respectively which is = and +1
job level of required position (i.e job level
3
). See below sample output
output_df
0 Pos_id Emp_id
1 1 15
2 2 11
3 3 13
4 5 15
5 6 11
6 6 12
Is there a simple way to do this in python pandas. Any help would be deeply appreciated.