Search code examples
pythonpandaspython-applymap

How to pass dataframes to a function using applymap() in pandas?


I am trying to use applymap() functionality in pandas to apply a function to a whole dataframe. I have a dataframe df (a sample of it is following):

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

I also have two other frames, namely, df_peak and df_off_peak which are following:

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']))

I have written a following function in a separate file interpolate.py:

Function to calculate losses corresponding to consumption

def cost_consump(consump,df_peak,df_off_peak):
    if(consump >= 0.459):
        for i in range(0, len(df_peak)):
            if(df_peak["Peak_Consumption"][i] > consump):
                cost = df_peak["Loss"][i-1]
                return cost        
                break;

    elif(consump <= 0.309):
        for i in range(0, len(df_off_peak)):
            if(df_off_peak["Off_Peak_Consumption"][i] > consump):
                cost = df_off_peak["Loss"][i-1]
                return cost        
                break;
    
    return 0

Then I call the function cost_consump as following:

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)

This works perfectly as I want. However I am curious if the job can be done by using applymap(). I tried to write following where I want to apply the function to dataframe df and save the resultant operation in df1.

Monthly_mean_cost = Monthly_mean_consump.transpose().applymap(cost_consump(**df_loss_peak,**df_loss_off_peak))

However, this requires me to pass two additional dataframes to the function which I am not sure how to do that.

I would appreciate any help.


Solution

  • I think this is a general question about how to pass arguments to the function used with applymap. Applymap passes each element in each series in a dataframe to the function passed. This is automatically passes as the first argument. You can write a function that incorporates this first element (x in the example below) and other dataframes or arguments.

    To call it, you can use a lambda function. An example is below.

    import numpy as np
    import pandas as pd
    df  = pd.DataFrame(np.random.randn(3, 3))
    peak = pd.DataFrame(np.random.randn(3, 3))
    off_peak = pd.DataFrame(np.random.randn(3, 3))
    
    def applymapFunction(x, df1, df2):
        if x > 0.5:
            return x * df1.mean()[0]
        else:
            return x * df2.mean()[0]
    
    df.applymap(lambda x: applymapFunction(x, peak, off_peak))