I have a data set that looks like this:
Cond Column_A Column_B Column_C Cumulative_Count
0 1 -0.60 -0.12 -0.17 1
1 0 0.30 0.70 0.98 0
2 1 -0.45 -0.71 -0.99 2
3 1 0.60 0.12 0.17 1
4 0 0.20 0.80 0.60 0
5 1 0.70 0.14 0.20 1
I would like to create a column Cumulative_Count that counts occurrence of an event based on multiple conditions such as:
1) If Cond=1 and (Column_A<0.5 or Column B>0.5) then Cumulative_Count=Cumulative_Count+1
2) If Cond=1 and (Column_B<0.5 or Column B>0.5) then Cumulative_Count=Cumulative_Count+1
3) If Cond=1 and (Column_C<0.5 or Column C>0.5) then Cumulative_Count=Cumulative_Count+1
I would like to use NumPy arrays to perform it because my dataset is very large. I tried using below code, it is not throwing error, but the result is not correct. And, I need to use it for all columns if possible because I have 50+ columns.
df['Cum_Count']=0
df['Cum_Count']=np.where((df['Cond']>0 & ((df['Column_A']<-0.5) | (df['Column_A']>0.5))), df['Cum_Count']+1, df['Cum_Count'])
Doing with
cond1=df.filter(like='Column')
cond2=df.Cond
df['count']=(cond1.gt(0.5)|cond1.lt(-0.5)).__and__(cond2,axis=0).sum(1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.