【问题标题】:Nested For loop Error Python嵌套 For 循环错误 Python
【发布时间】:2018-05-23 17:28:30
【问题描述】:

我正在处理从CSV 加载的数据框。 以下是数据集的链接: https://drive.google.com/open?id=1emA29M1lO3q-2YQOhegKRsAdlydBkgh6

我为相同的过程创建了一个示例示例。有效。但是当我尝试使用数据集时,会弹出如下错误:

TypeError: ufunc 'multiply' did not contain a loop with signature matching
types dtype('<U32') dtype('<U32') dtype('<U32')

我正在为数据框中的每一行运行 10 次随机计算。

for i in range(len(WC2018_Fix.HomeTeam)):
    for j in range(0,10):
        TeamA = WC2018_Fix.iloc[i,0]
        TeamB = WC2018_Fix.iloc[i,1]
        ELO_1 = np.array(WC2018_Fix.iloc[i,2])
        ELO_2 = np.array(WC2018_Fix.iloc[i,3])
        ELOA  = random.random()*ELO_1
        ELOB  = random.random()*ELO_2

谁能告诉我发生了什么?

【问题讨论】:

  • 你能告诉你你想达到什么目标吗?示例输入和输出也会很好。
  • 我尝试了示例示例,但它有效......当我加载数据框时它没有。
  • 对于for循环,我会将每个循环的结果保存在变量中。但当前的问题是循环中的错误。谢谢@zipa
  • 有什么问题?对您面临的错误有何建议?
  • 你用谷歌搜索过你的错误信息吗? stackoverflow.com/a/35016330/8881141 看来您的数据框中有字符串,而不是浮点数/整数。

标签: python pandas numpy for-loop


【解决方案1】:

在您的循环中,您可以检查您读入的值的格式/类型。 如果预期的单元格值不是 int/float,则相乘时,您可以键入 cast 然后执行此操作。

for i in range(len(WC2018_Fix.HomeTeam)):
    for j in range(0,10):
        TeamA = WC2018_Fix.iloc[i,0]
        TeamB = WC2018_Fix.iloc[i,1]
        if WC2018_Fix.iloc[i,2].__class__ == str:
          var = int(WC2018_Fix.iloc[i,2]) # Convert accordingly to int
        if WC2018_Fix.iloc[i,3].__class__ == str:
          var2 = int(WC2018_Fix.iloc[i,3])
        ELO_1 = np.array(var)
        ELO_2 = np.array(var2)
        ELOA  = random.random()*ELO_1
        ELOB  = random.random()*ELO_2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2012-10-10
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多