Home > database >  fastest way to access dataframe cell by colums values?
fastest way to access dataframe cell by colums values?

Time:05-27

I have the following dataframe :

time bk1_lvl0_id bk2_lvl0_id pr_ss order_upto_level initial_inventory leadtime1 leadtime2 adjusted_leadtime
0   2020    1000    3   16  18  17  3   0.100000    1
1   2020    10043   3   65  78  72  12  0.400000    1
2   2020    1005    3   0   1   1   9   0.300000    1
3   2020    1009    3   325 363 344 21  0.700000    1
4   2020    102 3   0   1   1   7   0.233333    1

I want a function to get the pr_ss for example for (bk1_lvl0_id=1000,bk2_lvl0_id=3). that's the code i've tried but it takes time :

def get_safety_stock(df,bk1,bk2):
##a function that returns the safety stock for any given (bk1,bk2)
for index,row in df.iterrows():
    if (row["bk1_lvl0_id"]==bk1) and (row["bk2_lvl0_id"]==bk2):
        return int(row["pr_ss"])
        break

CodePudding user response:

If your dataframe has no duplicate values based on bk1_lvl0_id and bk2_lvl0_id, You can make function as follows:

def get_safety_stock(df,bk1,bk2):
    return df.loc[df.bk1_lvl0_id.eq(bk1) & df.bk2_lvl0_id.eq(bk2), 'pr_ss'][0]

Note that its accessing the first value in the Series which shouldnt be an issue if there are no duplicates in data. If you want all of them, just remove the [0] from the end and it should give you the whole series. This can be called as follows:

get_safety_stock(df, 1000,3)
>>>16
  • Related