Concatenate 2 dataframes based on conditions

Question:

I have 2 dataframes, df_products_cp, like this one:

product_id version country
1111 2 CO
1111 2 BR
1111 2 MX
2222 2 CO
3333 2 CO
3333 2 MX
4444 2 CO
4444 2 BR
4444 2 MX

and df_products_ec like this one:

product_id version country
1111 3 CO
1111 3 MX
2222 3 CO
4444 3 CO
4444 3 BR

How can I concatenate both so that I only get a single dataframe like this one, when the product_id/country combination is found in both dataframes?

product_id version country
1111 2 CO
1111 3 CO
1111 2 MX
1111 3 MX
2222 2 CO
2222 3 CO
4444 2 CO
4444 3 CO
4444 2 BR
4444 3 BR
Asked By: Alain

||

Answers:

Let us do merge then wide_to_long

out = pd.wide_to_long(df1.merge(df2,on=['product_id','country']),
                      'version',
                      ['product_id','country'],
                      j = 'drop',
                      suffix = 'w+').reset_index().drop(['drop'],axis = 1)
Out[275]: 
   product_id country  version
0        1111      CO        2
1        1111      CO        3
2        1111      MX        2
3        1111      MX        3
4        2222      CO        2
5        2222      CO        3
6        4444      CO        2
7        4444      CO        3
8        4444      BR        2
9        4444      BR        3
Answered By: BENY
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.