Reputation: 1813
I am dealing with a dataframe which has several thousands of rows & has several columns. The column of interest is called customer_csate_score
.
The data looks like below
customer_csate_score
0.000
-0.4
0
0.578
0.418
-0.765
0.89
What I'm trying to do is create a new column in the dataframe called customer_csate_score_toggle_status
which will have true if the value changed either from a positive value to a negative value or vice-versa. It will have value false if the polarity didn't reverse.
Expected Output for toggle status column
customer_csate_score_toggle_status
False
True
False
True
False
True
True
I've tried few different things but haven't been able to get this to work. Here's what I've tried -
Attempt - 1
def update_cust_csate_polarity(curr_val, prev_val):
return True if (prev_val <= 0 and curr_val > 0) or (prev_val >= 0 and curr_val < 0) else False
data['customer_csate_score_toggle_status'] = data.apply(lambda x: update_cust_csate_polarity(data['customer_csate_score'], data['customer_csate_score'].shift()))
Attempt - 2
//Testing just one condition
data['customer_csate_score_toggle_status'] = data[(data['customer_csate_score'].shift() < 0) & (data['customer_csate_score']) > 0]
Could I please request help to get this right?
Upvotes: 0
Views: 109
Reputation: 214927
Calculate the sign change using np.sign(df.customer_csate_score)[lambda x: x != 0].diff() != 0
:
5 0 1
won't get marked incorrectly;diff
.import numpy as np
df['customer_csate_score_toggle_status'] = np.sign(df.customer_csate_score)[lambda x: x != 0].diff() != 0
df['customer_csate_score_toggle_status'] = df['customer_csate_score_toggle_status'].fillna(False)
df
customer_csate_score customer_csate_score_toggle_status
0 0.000 False
1 -0.400 True
2 0.000 False
3 0.578 True
4 0.418 False
5 -0.765 True
6 0.890 True
Upvotes: 1