python-3.x - 遍历两个列表/列,条件添加到第三个列表/列
问题描述
我正在做一个任务,我目前面临一个挑战,希望有人能够帮助我。
我有两列或列表(更适合的),我想迭代以将结果添加到第三列。它是自 2016 年原始数据库以来的英超联赛数据集,它将有条件设置获胜、失败和平局,将获得或不获得的积分添加到第 3 列/列表中。
谢谢
def pointsF(df):
x, y = list(df['score1']), list(df['score2'])
#x, y = iter(xl), iter(yl)
g = 0
r = list()
#for (i, z) in itertools.zip_longest(x, y):
for x, y in zip(x, y):
if x == y:
g = g +1
r.append(g)
next
elif x > y:
g = g+3
r.append(g)
next
else:
g = g
r.append(g)
next
return r
解决方案
如果有人遇到同样的情况,这是我发现的最好的解决方案!
cond1 = [ (dfPL['GoalsHome'] > dfPL['GoalsAway']), (dfPL['GoalsHome'] < dfPL['GoalsAway']), (dfPL['GoalsHome'] == dfPL['GoalsAway'])]
#choices result and points
cc = ['w','l','d']
ccp = [3,0,1]
dfPL['HomeResult'] = np.select(cond1, cc, default=0 )
推荐阅读
- c# - 有什么简单的方法可以从 datareader 填充列表模型吗?
- perl - 为什么这两个变量 `${ "_<$filename" }` 和 `${ "::_<$filename" }` 不相等?
- javascript - 专注于有错误的第一个输入。this.$refs.array[0] 有问题
- azure - 创建 webjob 后更改 CRON
- php - Symfony 3 - CKEditor 未正确安装在 PROD 中
- excel - 与屏幕缩放相关的 Excel UserForm 大小
- c# - 从 Visual Studio 项目中获取插入的项目名称
- java - 无法使用 IntelliJ 在远程树莓派上进行调试
- ios - iOS中的暗模式问题
- c++ - boost文件系统路径中的分段错误