title: How to pass dataframes to a function using applymap() in pandas?
body: <p>I am trying to use <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.applymap.html" rel="nofollow noreferrer"><code>applymap()</code></a> functionality in pandas to apply a function to a whole dataframe. I have a dataframe <code>df</code> (a sample of it is following):</p>
  <pre><code>Time    2010-08-31  2010-09-30  2010-10-31  2010-11-30  2010-12-31 2011-01-31  2011-02-28 2011-03-31  2011-04-30 
  00:00   0.429188    0.302406    0.587415    0.227820    0.115938    0.170616    0.056256    0.078167    0.476515
  00:30   0.340452    0.385037    0.218495    0.238118    0.134938    0.123279    0.054984    0.108111    0.173700
  01:00   0.455451    0.433700    0.229352    0.253046    0.391425    0.313715    0.401116    0.123304    0.453640
  01:30   0.601494    0.576142    0.425599    0.590769    0.486930    0.419002    0.560737    0.554705    0.544313
  02:00   0.504342    0.584523    0.614539    0.375294    0.377951    0.342675    0.357372    0.367332    0.391336
  02:30   0.527724    0.443303    0.457283    0.369515    0.392317    0.379410    0.391916    0.444807    0.491411
  03:00   0.429236    0.531203    0.464098    0.370421    0.426875    0.360209    0.276282    0.179577    0.304990
  03:30   0.442019    0.510604    0.314080    0.372268    0.443884    0.461649    0.390262    0.284042    0.417354
  </code></pre>
  <p>I also have two other frames, namely, <code>df_peak</code> and <code>df_off_peak</code> which are following:</p>
  <pre><code>df_peak = pd.DataFrame(np.random.randn(20,2), columns=list(['Peak Consumption', 'Loss']))
  df_off_peak = pd.DataFrame(np.random.randn(20,2), columns=list(['Off Peak Consumption', 'Loss']))
  </code></pre>
  <p>I have written a following function in a separate file <code>interpolate.py</code>:</p>
  <h1>Function to calculate losses corresponding to consumption</h1>
  <pre><code>def cost_consump(consump,df_peak,df_off_peak):
      if(consump &gt;= 0.459):
          for i in range(0, len(df_peak)):
              if(df_peak[&quot;Peak_Consumption&quot;][i] &gt; consump):
                  cost = df_peak[&quot;Loss&quot;][i-1]
                  return cost        
                  break;
  
      elif(consump &lt;= 0.309):
          for i in range(0, len(df_off_peak)):
              if(df_off_peak[&quot;Off_Peak_Consumption&quot;][i] &gt; consump):
                  cost = df_off_peak[&quot;Loss&quot;][i-1]
                  return cost        
                  break;
      
      return 0
  </code></pre>
  <p>Then I call the function <code>cost_consump</code> as following:</p>
  <pre><code>from interpolate import cost_consump
  df1 = pd.DataFrame.copy(df,deep=True)
  
  for i in range(0,48):
      for j in range(0,12):
          mean_consump = df.transpose().iloc[i,j]
          df1.transpose().iloc[i,j] =  cost_consump(mean_consump,df_loss_peak,df_loss_off_peak)
  </code></pre>
  <p>This works perfectly as I want. However I am curious if the job can be done by using <code>applymap()</code>. I tried to write following where I want to apply the function to dataframe <code>df</code> and save the resultant operation in <code>df1</code>.</p>
  <pre><code>Monthly_mean_cost = Monthly_mean_consump.transpose().applymap(cost_consump(**df_loss_peak,**df_loss_off_peak))
  </code></pre>
  <p>However, this requires me to pass two additional dataframes to the function which I am not sure how to do that.</p>
  <p>I would appreciate any help.</p>