这是示例数据。每当在Not win
列中看到Not fail
或Event
时,我想将Time
列的第1行的值更改为0。
x = pd.DataFrame({'Id': [102,303,944, 111, 675, 236], 'Event':['win', 'fail','Not fail','win','win','Not win'],
'Time':[10,22,0, 23, 45, 0]})
这就是结果的样子。
Event Id Time
0 win 102 10
1 fail 303 0
2 Not fail 944 0
3 win 111 23
4 win 675 0
5 Not win 236 0
答案 0 :(得分:3)
尝试一下
l=['Not win','Not fail']
mask=df['Event'].isin(l).shift(-1).fillna(False)
df.loc[mask,'Time']=0
输出:
Event Id Time
0 win 102 10
1 fail 303 0
2 Not fail 944 0
3 win 111 23
4 win 675 0
5 Not win 236 0
答案 1 :(得分:1)
x["isPreviousEventNotFailNotWin"] = ((x.Event == "Not fail") | (x.Event == "Not win")).shift(-1)
x.Time[x.isPreviousEventNotFailNotWin == True] = 0
答案 2 :(得分:1)
您可以通过以下方式使用where
:
l = ['Not fail', 'Not win']
x['Time'] = np.where(x['Event'].shift(-1).isin(l), 0, x['Time'])
输出:
Event Id Time
0 win 102 10
1 fail 303 0
2 Not fail 944 0
3 win 111 23
4 win 675 0
5 Not win 236 0
答案 3 :(得分:1)
使用带有pd.Series.shift
,后跟pd.DataFrame.loc
的布尔掩码:
mask = x['Event'].isin(['Not fail', 'Not win']).shift(-1).fillna(False)
x.loc[mask, 'Time'] = 0
print(x)
Event Id Time
0 win 102 10
1 fail 303 0
2 Not fail 944 0
3 win 111 23
4 win 675 0
5 Not win 236 0