因此,我正在網站上抓取一些網站,並查看替代數據,同時我想知道分數。所以我有當潛艇出現的時間和目標發生的時間。然後,我想要在替換的具體時間鏈接分數。這裏有一個例子:數據條件語句
import pandas as pd
df_stack = ['31:12',
'34:12',
'34:12',
'57:50',
'57:50',
'67:03',
'68:48',
'77:18',
'80:00',
'90:00']
# This df_stack that is commented works.
#df_stack = ['34:40', '36:53', '55:38', '56:03', '67:31', '74:43', '84:38',
# '86:58', '86:58']
In = ['a']*len(df_stack)
Out = ['b']*len(df_stack)
Subs = pd.DataFrame(data = [In,Out]).T
Subs.columns = ['In','Out']
Subs.index = [df_stack]
### This score works
#Score = ['0-0','0-1','1-1']
#Score = pd.DataFrame(data = [Score]).T
#Score.columns = ['Score']
#Score.index = ['61:37','61:38','81:45']
### This Score Doesn't Work
Score = ['0-0','0-1','1-1','2-1']
Score = pd.DataFrame(data = [Score]).T
Score.columns = ['Score']
Score.index = ['58:39', '58:40', '83:31', '89:41']
k = 0
j = 0
q = 0
overall_score = []
time = []
for i in Subs.index.tolist():
try:
if i < Score.index.tolist()[k]:
overall_score.append(Score['Score'][k])
time.append([Score.index[k],i,k,'top',Score['Score'][k]])
q += 1
else:
if (k > 0 and i > Score.index.tolist()[k] and i < Score.index.tolist()[k+1]):
overall_score.append(Score['Score'][k])
time.append([Score.index[k],i,Score.index[k+1],k,'No Change',q,Score['Score'][k]])
j += 1
q += 1
if (k == 0 and i > Score.index.tolist()[k]):
k += 1
q += 1
overall_score.append(Score['Score'][k])
time.append([Score.index[k],i,Score.index[k+1],k,'First Goal',Score['Score'][k]])
if (j >= 1 and i > Score.index.tolist()[k+j]):
h = 0
h += k + j
if k >= len(Score):
h = len(Score)-1
overall_score.append(Score['Score'][h])
time.append([Score.index[h],i,k,'Another Goal',j,Score['Score'][k]])
except IndexError:
#overall_score.append(Score['Score'][k-1])
overall_score.append(Score['Score'][len(Score)-1])
我知道這是一個很大的代碼,但overall_score的期望輸出應該是:
['0-0', '0-0', '0-0', '0-0', '0-0', '0-1', '0-1', '0-1','0-1' '2-1']
有可能做到這一點更簡單的方法,我也願意把整個刮碼在線,但它相當長。因此,與總分的替代會是什麼樣子:
In Out Score
31:12 a b 0-0
34:12 a b 0-0
34:12 a b 0-0
57:50 a b 0-0
57:50 a b 0-0
67:03 a b 0-1
68:48 a b 0-1
77:18 a b 0-1
80:00 a b 0-1
90:00 a b 2-1
我與解決方案1去了。我從來沒有想過創建一本字典,但它完美的作品。 –
我改變了解決方案的唯一一件事是我在函數條件語句中添加了時間> score_time或time == score_time。 –
@AdamWarner好的!你可以把它捲入'time> = score_time' :) –