I have the following table:

```
df = pd.DataFrame(({'code':['A121','A121','A121','H812','H812','H812','Z198','Z198','Z198','S222','S222','S222'],
                        'mode':['stk','sup','cons','stk','sup','cons','stk','sup','cons','stk','sup','cons'],
                        datetime.date(year=2021,month=5,day=1):[4,2,np.nan,2,2,np.nan,6,np.nan,np.nan,np.nan,2,np.nan],
                        datetime.date(year=2021,month=5,day=2):[1,np.nan,np.nan,3,np.nan,np.nan,2,np.nan,np.nan,np.nan,np.nan,np.nan],
                        datetime.date(year=2021,month=5,day=3):[12,5,np.nan,13,5,np.nan,12,np.nan,np.nan,np.nan,5,np.nan],
                        datetime.date(year=2021,month=5,day=4):[np.nan,1,np.nan,np.nan,4,np.nan,np.nan,np.nan,np.nan,np.nan,7,np.nan]}))
df = df.set_index('mode')

```

![](https://i.stack.imgur.com/r1wii.png)

---

I want to achieve the following, I want the the rows wherever `cons` to be set according to some arithemetic calculations:

`cons` for the corresponding date and code needs to be set to the following calculation `prev_date stk - current_date stk + sup`

I have tried the code below:

```
dates = list(df.columns)
dates.remove('code')
for date in dates:
    prev_date = date - datetime.timedelta(days=1)
    if(df.loc["stk"].get(prev_date,None) is not None):
        opn_stk = df.loc["stk",prev_date].reset_index(drop=True)
        cls_stk = df.loc["stk",date].reset_index(drop=True)
        sup = df.loc["sup",date].fillna(0).reset_index(drop=True)
        cons = opn_stk - cls_stk + sup
        df.loc["cons",date] = cons

```

I do not receive any error, **however the `cons` values does not change at all**.

I suspect this is probably because `df.loc["cons",date]` is an indexed Series and the calculation `opn_stk - cls_stk + sup` is an unindexed Series.
Any idea how to fix this?

P.S Also I am using loops to calculate this, is there any other vectorized way that would be more efficient

---

Expected Output

![](https://i.stack.imgur.com/oqyv6.png)