Creating a new column sequence based on another column within a dataframe

Question:

I have a dataframe Outfall which has a sequence in the column ['Head']. The goal is to then make another column relative to ['Head'] called ['OHead'] which starts a new sequence once a certain value is matched. I dont have any issues applying a lamda function to create the new column ['OHead'] based on a conditional but am getting tied up on then initiating a new sequence with the same step size as ['Head'].

To = 1.34
Head = np.linspace(0,4,49)
Outfall = pd.DataFrame(Head, columns=['Head'])
Outfall['OHead'] = Outfall['Head'].apply(lambda x: 0 if x <= To else i * 0.83 for i in range(1))

The list comprehension within the lamda statement obviously doesn’t work but was giving it a shot thinking this might be the best approach.

The expected result would be a dataframe that looks like this:

        Head         OHead
0   0.000000      0.000000
1   0.083333      0.000000
2   0.166667      0.000000
3   0.250000      0.000000
...n...      
15  1.250000      0.000000
16  1.333333      0.000000
17  1.416667      0.083333
18  1.500000      0.166667
19  1.583333      0.250000
20  1.666667      0.333333
21  1.750000      0.416667
...n...

Using …n… as place holders for the rest of the dataframe.

Asked By: rweber

||

Answers:

IIUC,

data = Outfall[Outfall <= To].dropna().tail(1).reset_index(drop=True).at[0,'Head']
Outfall['Head'].apply(lambda x: 0 if x <= data else x - data)

or

data = Outfall[Outfall <= To].dropna().tail(1).reset_index(drop=True).at[0,'Head']
Outfall['OHead'] = np.where(Outfall['Head'] <= data, 0, Outfall['Head'] - data)
Answered By: MoRe