1

I have two Data Frame. I wanna merge them but I don't find any common column in between.

df1

Date           var1   var2    var3   var4 
2022/01/01     10     100     110    50
2022/01/02     11     110     100    65
       

df2

 IDV_Key   IDV_Name
  var1     Variable 1
  var2     Variable 2
  var3     Variable 3
  var4     Variable 4

Desired output Data Frame would be:

df

Date           IDV       Value    IDV_Key    IDV_Name
2022/01/01     var1       10       var1      Variable 1
2022/01/01     var2       100      var2      Variable 2
2022/01/01     var3       110      var3      Variable 3
2022/01/01     var4       50       var4      Variable 4
2022/02/01     var1       11       var1      Variable 1
2022/02/01     var2       110      var2      Variable 2 
2022/02/01     var3       100      var3      Variable 3 
2022/02/01     var4       65       var4      Variable 4

Having issue since doesn't have any common unique columns on top of I can merge it.

I tried below commands:

df1 = df1.stack.reset_index(drop = True, inplace = True)  
df1.columns = [IDV, Value]   
df1['IDV_Name'] = df2['IDV_Name']  
df1
Amit Kumar
  • 154
  • 12
  • I see common `IDV` column, do you try merge? – jezrael Mar 29 '22 at 07:41
  • 1
    You need explain why merge not working. – jezrael Mar 29 '22 at 07:41
  • What is the output of `df1.merge(df2, on='IDV')`? If nothing, please **provide the full constructors for both datasets**. First ensure that there are no spaces or else that make it look identical even if they are in fact different – mozway Mar 29 '22 at 07:42
  • so need `df = df1.merge(df2, left_on='IDV', right_on='IDV_Key', how='left')` ? – jezrael Mar 29 '22 at 07:47
  • check `melt` or `stack` in added dupe. – jezrael Mar 29 '22 at 07:53
  • @mozway Please check now – Amit Kumar Mar 29 '22 at 07:53
  • @Amit "It doesn't work" or "it doesn't solve the problem" is not a valid reason. You need to **provide the explicit command** that you tried, and **describe how it doesn't meet your expectations** – mozway Mar 29 '22 at 07:55
  • @mozway I tried this `df1 = df1.stack.reset_index(drop = True, inplace = True) df1.columns = [IDV, Value] df1['IDV_Name'] = df2['IDV_Name'] df1` – Amit Kumar Mar 29 '22 at 08:08
  • As mentioned above, edit the question with those details, provide the inputs as dataframe constructors, provide the error or details on the unwanted behavior – mozway Mar 29 '22 at 08:12

0 Answers0