python-3.x - IF 语句应用不同的公式

标签 python-3.x pandas

我希望创建一个字段来告诉我一名球员在 FPL 中通过进球或助攻获得了多少分。由于每个进球根据位置(门将、后卫、中场、前锋)有不同的分数,但助攻给予相同数量的分数(3),所以我需要代码来识别球员的位置并根据分数相乘他每个位置都会得到。

代码如下:

multi_goal = if df['element_type'] == 'FWD': 4
            elif df['element_type'] == 'MID': 5
            elif df['element_type'] == 'DEF': 6
            else df['element_type'] == 'GK': 6

goals_int = df['goal_part'] = (df['goals_scored']*multigoal) + (df['assists']*3)

“多目标”对象给出此错误:

multi_goal = if df['element_type'] == 'FWD': 4
              ^
SyntaxError: invalid syntax

最佳答案

您可以使用更简单的合成器,例如:

if df['element_type'] == 'FWD':
    multi_goal = 4
elif df['element_type'] == 'MID':
    multi_goal = 5
...

或者您可以实现 switch 语句,https://jaxenter.com/implement-switch-case-statement-python-138315.html

希望有帮助;)

关于python-3.x - IF 语句应用不同的公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59382204/

相关文章:

python-3.x - 如何在 python 中使用 opencv 读取数据矩阵代码?

python - python/pandas 和大左外连接的 MemoryError

python - 为什么我的就地反转字符列表的解决方案不起作用?

python - 基于月份的累计列数

python - 如何在Python中反转子列表

python - Pandas .at 与 .loc

python - 如何在pandas中获取所选行与另一列值的平均值

python - 两点层之间的距离矩阵

python - 以秒为单位将整列转换为小时( Pandas )

python - 使用 f 字符串固定小数点后的数字