【发布时间】:2017-07-14 06:02:36
【问题描述】:
我有一个大型 NFL 场景数据集,但为了便于说明,我将其简化为包含 2 个观察值的列表。像这样:
data = [[scenario1],[scenario2]]
以下是数据集的组成:
data[0][0]
>>"It is second down and 3. The ball is on your opponent's 5 yardline. There is 3 seconds left in the fourth quarter. You are down by 3 points."
data[1][0]
>>"It is first down and 10. The ball is on your 20 yardline. There is 7 minutes left in the third quarter. You are down by 10 points."
我无法用这样的字符串格式的数据构建任何模型。因此,我想将这些场景重新编码为新的列(或者如果你愿意的话)作为定量值。我想我应该先把数据框弄平:
down = 0
yards = 0
yardline = 0
seconds = 0
quarter = 0
points = 0
data = [[scenario1, down, yards, yardline, seconds, quarter, points], [scenario2, yards, yardline, seconds, quarter, points]]
现在是棘手的部分,我必须如何从场景列的信息中填充新列。棘手,因为例如,在第二句中,如果出现“对手”这个词,这意味着我们必须将其计算为 100——无论码线编号是多少。在上面的scenario1变量中,应该是100-5=95。
起初我以为我应该把所有的数字分开并扔掉单词,但正如上面所指出的,有些单词实际上是正确分配数量值所必需的。我从来没有做过这么微妙的 lambda。或者,lambda 不是正确的方法?我愿意接受任何/所有建议。
为了强化,这里是我想看到的(如果我输入来自scenario1:
data[0][1:]
>>2,3,95,3,4,-3
谢谢
【问题讨论】:
标签: string list python-3.x lambda