满足条件时跳出'nested loop',继续父循环的循环
Break out of the 'nested loop' when condition is met, and then continue the loop of the parent loop
我有一个嵌套循环,但我只需要子循环的第一个条件。所以我需要子循环在满足条件时停止,并为父循环的索引重新启动循环。这个例子应该清楚。我有数据框的前几行:
M# Date Time Day Team Team2 Venue Team3 days_between1 next_game1
26 27 2021-05-01 7.30 pm Sat MI CSK Delhi MI CSK 0 0
27 28 2021-05-02 3.30 pm Sun RR SRH Delhi RR SRH 0 0
28 29 2021-05-02 7.30 pm Sun PK DC Ahmedabad PK DC 0 0
29 30 2021-05-03 7.30 pm Mon KKR RCB Ahmedabad KKR RCB 0 0
30 31 2021-05-04 7.30 pm Tue SRH MI Delhi SRH MI 0 0
31 32 2021-05-05 7.30 pm Wed RR CSK Delhi RR CSK 0 0
32 33 2021-05-06 7.30 pm Thur RCB PK Ahmedabad RCB PK 0 0
33 34 2021-05-07 7.30 pm Fri SRH CSK Delhi SRH CSK 0 0
34 35 2021-05-08 3.30 pm Sat KKR DC Ahmedabad KKR DC 0 0
我正在尝试计算特定球队参加的比赛之间的天数。例如,MI 播放第一个和第四个。我创建了一个 'Team3' 列,其中包含两支球队的名称,以便更轻松地设置条件。这是我的尝试:
for i in range(26, df.last_valid_index()):
a = df['Team'][i]
for j in range(i,df.last_valid_index()):
t = df['Team3'][j]
if t.find(a) != -1:
df['days_between1'][i] = df['Date'][j] - df['Date'][i]
结果应如下所示:
M# Date Time Day Team Team2 Venue Team3 days_between1 next_game1
26 27 2021-05-01 7.30 pm Sat MI CSK Delhi MI CSK 3 0
27 28 2021-05-02 3.30 pm Sun RR SRH Delhi RR SRH 3 0
28 29 2021-05-02 7.30 pm Sun PK DC Ahmedabad PK DC 3 0
29 30 2021-05-03 7.30 pm Mon KKR RCB Ahmedabad KKR RCB 3 0
30 31 2021-05-04 7.30 pm Tue SRH MI Delhi SRH MI 1 0
31 32 2021-05-05 7.30 pm Wed RR CSK Delhi RR CSK NA 0
32 33 2021-05-06 7.30 pm Thur RCB PK Ahmedabad RCB PK NA 0
33 34 2021-05-07 7.30 pm Fri SRH CSK Delhi SRH CSK NA 0
34 35 2021-05-08 3.30 pm Sat KKR DC Ahmedabad KKR DC NA 0
您可以将循环移动到 DataFrame.apply()
。
找到与给定 team
匹配的 first_valid_index()
并从 team
的日期中减去匹配的日期:
def between(row):
index, team = row.name, row.Team
mask = df[['Team', 'Team2']].loc[index+1:] == team
match = mask.replace(False, np.nan).first_valid_index()
return df.loc[match, 'Date'] - df.loc[index, 'Date'] if match else np.nan
df.days_between1 = df.apply(between, axis=1)
# M# Date Time Day Team Team2 Venue Team3 days_between1 next_game1
# 26 27 2021-05-01 7.30 pm Sat MI CSK Delhi MI CSK 3 days 0
# 27 28 2021-05-02 3.30 pm Sun RR SRH Delhi RR SRH 3 days 0
# 28 29 2021-05-02 7.30 pm Sun PK DC Ahmedabad PK DC 4 days 0
# 29 30 2021-05-03 7.30 pm Mon KKR RCB Ahmedabad KKR RCB 5 days 0
# 30 31 2021-05-04 7.30 pm Tue SRH MI Delhi SRH MI 3 days 0
# 31 32 2021-05-05 7.30 pm Wed RR CSK Delhi RR CSK NaT 0
# 32 33 2021-05-06 7.30 pm Thur RCB PK Ahmedabad RCB PK NaT 0
# 33 34 2021-05-07 7.30 pm Fri SRH CSK Delhi SRH CSK NaT 0
# 34 35 2021-05-08 3.30 pm Sat KKR DC Ahmedabad KKR DC NaT 0
我有一个嵌套循环,但我只需要子循环的第一个条件。所以我需要子循环在满足条件时停止,并为父循环的索引重新启动循环。这个例子应该清楚。我有数据框的前几行:
M# Date Time Day Team Team2 Venue Team3 days_between1 next_game1
26 27 2021-05-01 7.30 pm Sat MI CSK Delhi MI CSK 0 0
27 28 2021-05-02 3.30 pm Sun RR SRH Delhi RR SRH 0 0
28 29 2021-05-02 7.30 pm Sun PK DC Ahmedabad PK DC 0 0
29 30 2021-05-03 7.30 pm Mon KKR RCB Ahmedabad KKR RCB 0 0
30 31 2021-05-04 7.30 pm Tue SRH MI Delhi SRH MI 0 0
31 32 2021-05-05 7.30 pm Wed RR CSK Delhi RR CSK 0 0
32 33 2021-05-06 7.30 pm Thur RCB PK Ahmedabad RCB PK 0 0
33 34 2021-05-07 7.30 pm Fri SRH CSK Delhi SRH CSK 0 0
34 35 2021-05-08 3.30 pm Sat KKR DC Ahmedabad KKR DC 0 0
我正在尝试计算特定球队参加的比赛之间的天数。例如,MI 播放第一个和第四个。我创建了一个 'Team3' 列,其中包含两支球队的名称,以便更轻松地设置条件。这是我的尝试:
for i in range(26, df.last_valid_index()):
a = df['Team'][i]
for j in range(i,df.last_valid_index()):
t = df['Team3'][j]
if t.find(a) != -1:
df['days_between1'][i] = df['Date'][j] - df['Date'][i]
结果应如下所示:
M# Date Time Day Team Team2 Venue Team3 days_between1 next_game1
26 27 2021-05-01 7.30 pm Sat MI CSK Delhi MI CSK 3 0
27 28 2021-05-02 3.30 pm Sun RR SRH Delhi RR SRH 3 0
28 29 2021-05-02 7.30 pm Sun PK DC Ahmedabad PK DC 3 0
29 30 2021-05-03 7.30 pm Mon KKR RCB Ahmedabad KKR RCB 3 0
30 31 2021-05-04 7.30 pm Tue SRH MI Delhi SRH MI 1 0
31 32 2021-05-05 7.30 pm Wed RR CSK Delhi RR CSK NA 0
32 33 2021-05-06 7.30 pm Thur RCB PK Ahmedabad RCB PK NA 0
33 34 2021-05-07 7.30 pm Fri SRH CSK Delhi SRH CSK NA 0
34 35 2021-05-08 3.30 pm Sat KKR DC Ahmedabad KKR DC NA 0
您可以将循环移动到 DataFrame.apply()
。
找到与给定 team
匹配的 first_valid_index()
并从 team
的日期中减去匹配的日期:
def between(row):
index, team = row.name, row.Team
mask = df[['Team', 'Team2']].loc[index+1:] == team
match = mask.replace(False, np.nan).first_valid_index()
return df.loc[match, 'Date'] - df.loc[index, 'Date'] if match else np.nan
df.days_between1 = df.apply(between, axis=1)
# M# Date Time Day Team Team2 Venue Team3 days_between1 next_game1
# 26 27 2021-05-01 7.30 pm Sat MI CSK Delhi MI CSK 3 days 0
# 27 28 2021-05-02 3.30 pm Sun RR SRH Delhi RR SRH 3 days 0
# 28 29 2021-05-02 7.30 pm Sun PK DC Ahmedabad PK DC 4 days 0
# 29 30 2021-05-03 7.30 pm Mon KKR RCB Ahmedabad KKR RCB 5 days 0
# 30 31 2021-05-04 7.30 pm Tue SRH MI Delhi SRH MI 3 days 0
# 31 32 2021-05-05 7.30 pm Wed RR CSK Delhi RR CSK NaT 0
# 32 33 2021-05-06 7.30 pm Thur RCB PK Ahmedabad RCB PK NaT 0
# 33 34 2021-05-07 7.30 pm Fri SRH CSK Delhi SRH CSK NaT 0
# 34 35 2021-05-08 3.30 pm Sat KKR DC Ahmedabad KKR DC NaT 0