Home > OS >  Calculating a parameter based on values from the previous day
Calculating a parameter based on values from the previous day

Time:11-30

I have a DataFrame which is part of a wider script on Python with the following characteristics:

  • index dtype='datetime64[ns]', length=365, freq='D'
  • daily values for different parameters
  • a default value for DOY 1
  • a pair of values I previously calculated for some specific dates.

To date, the dataframe structure is something like this:

                 col1    col2      col3       col4       Condition   value to calculate
2018-01-01        5.0  0.853232  0.992774   0.65377        0.0       0.65429
2018-01-02        0.4  0.004652  0.992774   0.65377        0.0       NaN
2018-01-03        0.0  0.000000  0.992774   0.65377        0.0       NaN
2018-01-04        0.0  0.000000  0.992774   0.65377        1.0       0.33456
2018-01-05        0.4  0.004652  0.992774   0.65377        0.0       NaN
              ...       ...       ...       ...        ...       ...

The 'condition' column was used to attribute values at a specific time in the last column

The missing values in the last column should be calculated using data from columns 1 to 4, using values of the previous DOY from both columns 1 to 4 and from the same column of the value that must me calculated.

Also, in case a value is already present at a specific DOY, the formula should reset to start calculation using the previously added value.

I'm not sure how to put this in a loop to execute the operation, or if there are other alternatives to resolve this part of the code I'm working on.

CodePudding user response:

It seems like you need something like this: def compute_value(previous_row, row):

for index, row in df.iterrows():
      if index>0:
           previous_row = df.iloc[index-1]
           row['value to calculate'] = previous_row['col1'] * previous_row['col2'] - previous_row['col3'] * previous_row['col4']

This is how you iterate but not sure to have fully understood the condition and not sure what you meant with, let me know if I can expand

Also, in case a value is already present at a specific DOY, the formula should reset to start calculation using the previously added value.

  • Related