简体   繁体   中英

look for patterns in a column of pandas dataframe based on the value of other column

I have the following dataframe: 在此处输入图像描述

in each row where key==1, I would like to search s_w column for two occurrences of 1 before and after that row( where key==1) then sum value of v for those rows and put it in a new column X. These occurrences of 1s should not be necessarily successive, there can be a gap between is in s_w column for example 11....11 or 101....10001, but if we fail to find two 1s in s_w column in either before or after that row ( where key==1) then we put NaN in X column. also NaN for rows where key==0.

EDIT: a new dataframe to test if solution generalize well:

 df = pd.DataFrame( { "p":[1,1,1,1,1,1,1,1,1,1,1,1,1],
                 "l" :[1,1,1,1,1,1,1,1,1,1,1,1,1],
                 "w":[1,2,3,4,5,6,7,8,9,10,11,12,12],
                 "s_w":[1,1,0,0,0,0,1,0,0,0,0,1,1],
                 "key" :[1,1,0,0,0,1,0,1,0,0,0,0,1],
                 "v":[2,2,5,3,4,5,5,1,2,3,4,5,4]
               })

I think here is necessary add mask only by Series.where added to previous answer :

g = df[df['s_w'].eq(1)].groupby(['p','l'])['v']
df['c_s'] = g.shift(-1).add(g.shift(-2)).add(g.shift(2)).add(g.shift(1)).where(df['key'].eq(1))


print (df)
    p  l   w  s_w  key  v   c_s
0   1  1   1    1    1  2   NaN
1   1  1   2    1    1  2   NaN
2   1  1   3    0    0  5   NaN
3   1  1   4    0    0  3   NaN
4   1  1   5    0    0  4   NaN
5   1  1   6    1    1  5  10.0 <- 2 + 2 + 5 + 1
6   1  1   7    1    0  5   NaN
7   1  1   8    1    1  1  19.0 <- 5 + 5 + 5 + 4
8   1  1   9    0    0  2   NaN
9   1  1  10    0    0  3   NaN
10  1  1  11    0    0  4   NaN
11  1  1  12    1    0  5   NaN
12  1  1  12    1    1  4   NaN

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM