How to apply a function on every row on a dataframe?

后端 未结 1 616
难免孤独
难免孤独 2020-12-03 13:00

I am new to Python and I am not sure how to solve the following problem.

I have a function:

def EOQ(D,p,ck,ch):
    Q = math.sqrt((2*D*ck)/(ch*p))
           


        
相关标签:
1条回答
  • 2020-12-03 13:47

    The following should work:

    def EOQ(D,p,ck,ch):
        Q = math.sqrt((2*D*ck)/(ch*p))
        return Q
    ch=0.2
    ck=5
    df['Q'] = df.apply(lambda row: EOQ(row['D'], row['p'], ck, ch), axis=1)
    df
    

    If all you're doing is calculating the square root of some result then use the np.sqrt method this is vectorised and will be significantly faster:

    In [80]:
    df['Q'] = np.sqrt((2*df['D']*ck)/(ch*df['p']))
    
    df
    Out[80]:
        D   p          Q
    0  10  20   5.000000
    1  20  30   5.773503
    2  30  10  12.247449
    

    Timings

    For a 30k row df:

    In [92]:
    
    import math
    ch=0.2
    ck=5
    def EOQ(D,p,ck,ch):
        Q = math.sqrt((2*D*ck)/(ch*p))
        return Q
    
    %timeit np.sqrt((2*df['D']*ck)/(ch*df['p']))
    %timeit df.apply(lambda row: EOQ(row['D'], row['p'], ck, ch), axis=1)
    1000 loops, best of 3: 622 µs per loop
    1 loops, best of 3: 1.19 s per loop
    

    You can see that the np method is ~1900 X faster

    0 讨论(0)
提交回复
热议问题