1

Have data in dataframe and need to compare current value of one column and prior of value of another column. Current time is row 5 in this dataframe and here's the desired output:

target data is streamed and captured into a DataFrame, then that array is multiplied by a constant to generate another column, however unable to generate the third column comp, which should compare current value of prod with prior value of the comp from comp.

df['temp'] = self.temp
df['prod'] = df['temp'].multiply(other=const1)

Another user had suggested using this logic but it is generates errors because the routine's array doesn't match the size of the DataFrame:

for i in range(2, len(df['temp'])):
    df['comp'].append(max(df['prod'][i], df['comp'][i - 1]))

enter image description here

It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
mbmt
  • 53
  • 6

1 Answers1

1

Let's try this, I think this will capture your intended logic:

df = pd.DataFrame({'col0':[1,2,3,4,5]
                  ,'col1':[5,4.9,5.5,3.5,6.3]
                  ,'col2':[2.5,2.45,2.75,1.75,3.15]
                  })

df['col3'] = df['col2'].shift(-1).cummax().shift()

print(df)

Output:

   col0  col1  col2  col3
0     1   5.0  2.50   NaN
1     2   4.9  2.45  2.45
2     3   5.5  2.75  2.75
3     4   3.5  1.75  2.75
4     5   6.3  3.15  3.15
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Scott, again many thanks for the solution. It works! Advancing the problem a step further, what if df needs to be reset as a cycle of data is reset? I tried to put df inside __init__ but have had trouble implementing it. The use case is as follows: data comes in for col1, and col3 calculates cummax and shifts appropriately...once action is taken on the current cummax and a data cycle is complete, a new cycle is to restart (resetting the df). The trigger for resetting could be something like col1 exceeding col3 or some set-point that col3 exceeds. – mbmt Dec 30 '19 at 23:51
  • @mbmt... Can you present this problem as a new question with expanded data that shows your new cycle. Also, include expected output from this data. This will help me and others in the SO community get you the proper answer. https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples Thank you. I think you need to add a groupby somewhere in there to get the new cycles. – Scott Boston Dec 31 '19 at 00:30
  • Hi Scott, yes will do..just after I posted the q, I realized it should actually be a new request because you already solved the first – mbmt Dec 31 '19 at 00:32
  • 1
    Hi Scott, I posted another q with same topic and appended “(and resetting evaluations)” to the subject..I think your groupby suggestion might do it..I’ve tried but can’t get the syntax to work – mbmt Jan 03 '20 at 02:29
  • @mbmt Sorry, it took me a while to get around to this new question. See my proposed solution [here](https://stackoverflow.com/a/59576959/6361531). – Scott Boston Jan 03 '20 at 10:35