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))     return Q   Say I have the dataframe
df = pd.DataFrame({"D": [10,20,30], "p": [20, 30, 10]})      D   p 0   10  20 1   20  30 2   30  10  ch=0.2 ck=5   And ch and ck are float types. Now I want to apply the formula to every row on the dataframe and return it as an extra row 'Q'. An example (that does not work) would be:
df['Q']= map(lambda p, D: EOQ(D,p,ck,ch),df['p'], df['D'])    (returns only 'map' types)
I will need this type of processing more in my project and I hope to find something that works.
Python's Pandas Library provides an member function in Dataframe class to apply a function along the axis of the Dataframe i.e. along each row or column i.e. Important Arguments are: func : Function to be applied to each column or row. This function accepts a series and returns a series.
Pandas. dataframe. apply() function is used to apply the function along the axis of a DataFrame. Objects passed to that function are Series objects whose index is either a DataFrame's index (axis=0) or a DataFrame's columns (axis=1).
Pandas DataFrame apply() MethodThe apply() method allows you to apply a function along one of the axis of the DataFrame, default 0, which is the index (row) axis.
Pandas DataFrame apply() is a library function that allows the users to pass a function and apply it to every value of the Series. To apply a function to every row in a Pandas DataFrame, use the Pandas df. apply() function.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With