Skip to content Skip to sidebar Skip to footer

How To Include Two Lambda Operations In Transform Function?

I have a dataframe like as given below df = pd.DataFrame({ 'date' :['2173-04-03 12:35:00','2173-04-03 17:00:00','2173-04-03 20:00:00','2173-04-04 11:00:00','2173-04-04 12:00:00','2

Solution 1:

You have a few things to fix First, in logic_2, you have lambda x but use y, so, you got to change that as below

logic_2 = lambda y: (y.shift(1).ge(1)) & (y.shift(2).ge(2)) & (y.shift(-1).ge(1))

Then you can use the logic's together as below' No need to create a blank column label. You can create the '`label' column directly as below.

df['label'] = ((df.groupby('subject_id')['val'].transform(logic_1))
           & (df.groupby('subject_id')['tdiff'].transform(logic_2))).map({True:'0',False:'1'})

Note: You logic produces all False values. So, you will get 1's if False is mapped to 1, not True

Post a Comment for "How To Include Two Lambda Operations In Transform Function?"