title: Map values from one dataframe to new columns in other based on column values - Pandas
body: <p>I have a problem with mapping values from another dataframe. </p>
  
  <p>These are samples of two dataframes:</p>
  
  <p>df1</p>
  
  <pre><code>product   class_1   class_2   class_3
  141A        11        13         5     
  53F4        12        11        18  
  GS24        14        12        10   
  </code></pre>
  
  <p>df2</p>
  
  <pre><code>id    product_type_0  product_type_1 product_type_2  product_type_3 measure_0 measure_1 measure_2   measure_3
  1         141A            GS24             NaN           NaN          1         3           NaN       NaN
  2         53F4            NaN              NaN           NaN          1        NaN          NaN       NaN
  3         53F4            141A             141A          NaN          2         2            1        NaN
  4         141A            GS24             NaN           NaN          3         2           NaN       NaN
  </code></pre>
  
  <p>What I'm trying to get is next:
   I need to add a new columns called "Max_Class_1", "Max_Class_2", "Max_Class_3" and that value would be taken from df1.
  For each order number (_1, _2, _3) look at existing columns (for example product_type_1) product_type_1 and take a row from df1 where the product has the same value. Then look at the measure columns (for example measure_1) and if the value is 1 (it's possible max four different values in original data), new column called "Max_Class_1" would have value same as class_1 for that product_type, in this case 11. </p>
  
  <p>I think it's a little bit simpler than I explained it. </p>
  
  <p>Desired output</p>
  
  <pre><code>id    product_type_0  product_type_1 product_type_2  product_type_3  measure_0 measure_1 measure_2  measure_3  max_class_0  max_class_1  max_class_2  max_class_3
  1         141A            GS24             NaN         NaN            1         3         NaN        NaN        1           10            NaN NaN
  2         53F4            NaN              NaN         NaN            1        NaN        NaN        NaN        12         NaN           NaN  NaN
  3         53F4            141A             141A        NaN            2         2         1          NaN        11          13            11  NaN
  4         141A            GS24             NaN         NaN            3         2         NaN        NaN        5           12            NaN NaN
  </code></pre>
  
  <p>The code I have tried with:</p>
  
  <pre><code>df2['max_class_1'] = None
  df2['max_class_2'] = None
  df2['max_class_3'] = None
  
  def get_max_class(product_df, measure_df, product_type_column, measure_column, max_class_columns):
      for index, row in measure_df.iterrows():
          product_df_new = product_df[product_df['product'] == row[product_type_column]]
          for ind, r in product_df_new.iterrows():
              if row[measure_column] == 1:
                  row[max_class_columns] = r['class_1']
              elif row[measure_column] == 2:
                  row[max_class_columns] = r['class_2']
              elif row[measure_column] == 3:
                  row[max_class_columns] = r['class_3']
              else:
                  row[tilt_column] = "There is no measure or type"
      return measure_df
  
  # And the function call 
  first_class = get_max_class(product_df=df1, measure_df=df2, product_type_column=product_type_1, measure_column='measure_1', max_class_columns='max_class_1')
  
  second_class = get_max_class(product_df=df1, measure_df=first_class, product_type_column=product_type_2, measure_column='measure_2', max_class_columns='max_class_2')
  
  third_class = get_max_class(product_df=df1, measure_df=second_class, product_type_column=product_type_3, measure_column='measure_3', max_class_columns='max_class_3')
  </code></pre>
  
  <p>I'm pretty sure there is a simpler solution, but don't know why is not working. I'm getting all None values, nothing changes. </p>