Q:

pandas apply function to every row

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
3
# Get rid of $ and , in the SAL-RATE, then convert it to a float
def money_to_float(money_str):
    return float(money_str.replace("$","").replace(",",""))

df['SAL-RATE'].apply(money_to_float)
0
# Import pandas package  
import pandas as pd 
  
# Function to add 
def add(a, b, c): 
    return a + b + c 
  
def main(): 
      
    # create a dictionary with 
    # three fields each 
    data = { 
            'A':[1, 2, 3],  
            'B':[4, 5, 6],  
            'C':[7, 8, 9] } 
      
    # Convert the dictionary into DataFrame  
    df = pd.DataFrame(data) 
    print("Original DataFrame:\n", df) 
      
    df['add'] = df.apply(lambda row : add(row['A'], 
                     row['B'], row['C']), axis = 1) 
   
    print('\nAfter Applying Function: ') 
    # printing the new dataframe 
    print(df) 
   
if __name__ == '__main__': 
    main() 
-1

New to Communities?

Join the community