因此,我正在网站上抓取一些网站,并查看替代数据,同时我想知道分数。所以我有当潜艇出现的时间和目标发生的时间。然后,我想要在替换的具体时间链接分数。这里有一个例子:数据条件语句
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' :) –