how to add results of function in different datafame columns

Viewed 27

My goal is to go from this dataframe called slc containg all the informations about run_id,bk1,bk2 :

run_id bk1 bk2
2 1000 3
4 1890 3

to this result :

run_id bk1 bk2 avg_inv proc shortage
2 1000 3 32,33 43 5
4 1890 3 45,22 4 17

Where avg_inv,proc and shortage are outputs of this function:

def order_up_to(demand_df,info_df,run,bk1,bk2):
...........
return proc,avg_inv,shortage

this function is calling other functions similar to this :

def get_initial_inventory_level(df,bk1,bk2):
##a function that returns the initial inventory level for any given (bk1,bk2)
return int(df[(df["bk1_lvl0_id"]==bk1) & (df["bk2_lvl0_id"]==bk2)]["initial_inventory"])

and:

When i test order_up_to function(dmd,df,2,1000,3) it works and it returns 3 integers but when i tried to create a datafame containing all run_id,bk1,bk2 and tried to add differnet columns representing the output of the order_up_to function using this code :

def f(row):
return(order_up_to(dmd,df,row["run_id"],row["bk1"],row["bk2"]))
slc["order_up_to"]=slc.apply(lambda x: f(x), axis=1)  

I got the following error :

    ---> 3     return int(df[(df["bk1_lvl0_id"]==bk1) & (df["bk2_lvl0_id"]==bk2)]["initial_inventory"])
TypeError: cannot convert the series to <class 'int'>
0 Answers
Related