【问题标题】:how do I resolve the following error - TypeError: unsupported operand type(s) for -: 'int' and 'str'如何解决以下错误 - TypeError: unsupported operand type(s) for -: 'int' and 'str'
【发布时间】:2020-08-17 23:31:34
【问题描述】:

我的代码中出现以下错误。 Confirmed_India、recovered_India、deaths_India 是包含针对电晕病例的预定义数据集的表列表。代码:

confirmed_India_ts = confirmed_df[confirmed_df['Country/Region'] == "India"]

confirmed_India_ts = confirmed_India_ts.drop(
['Lat','Long','Country/Region','Province/State'],axis=1).reset_index(drop=True).sum()

deaths_India_ts = deaths_df[deaths_df['Country/Region'] == "India"]

confirmed_India_ts = deaths_India_ts.drop(
['Lat','Long','Country/Region','Province/State'],axis=1).reset_index(drop=True).sum()

recovered_India_ts = recovered_df[recovered_df['Country/Region'] == "India"]

recovered_India_ts = deaths_India_ts.drop(
['Lat','Long','Country/Region','Province/State'],axis=1).reset_index(drop=True).sum()

active_India_ts = pd.Series(
    data = np.array(
            
[(x1 - x2 - x3) for (x1, x2, x3) in zip(
                confirmed_India_ts.values, deaths_India_ts.values, 

recovered_India_ts.values)
               
 ]
                
        ),
        index = confirmed_India_ts.index
)





error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-68268b5b4ce7> in <module>
 14         data = np.array(
 15                 [(x1 - x2 - x3) for (x1, x2, x3) in zip(
---> 16                     confirmed_India_ts.values, deaths_India_ts.values,      recovered_India_ts.values)
 17                     ]
 18 

 <ipython-input-54-68268b5b4ce7> in <listcomp>(.0)
 13 active_India_ts = pd.Series(
 14         data = np.array(
---> 15                 [(x1 - x2 - x3) for (x1, x2, x3) in zip(
 16                     confirmed_India_ts.values, deaths_India_ts.values, recovered_India_ts.values)
 17                     ]

TypeError: 不支持的操作数类型 -: 'int' 和 'str'

【问题讨论】:

  • (x1 - x2 - x3) 的预期输出是什么?

标签: python string int operands unsupportedoperation


【解决方案1】:

这个错误提示你试图减去 Integer 类型和 String 类型。因此,我建议您在执行 (x1 - x2 - x3) 操作之前检查每个 x1、2、3 RIGHT 的数据类型。也许您在预处理过程中弄乱了其中一种类型。例如。 x1 是 Int 类型,而 x2 是字符串。

请提供更多信息,以便我可以帮助您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 2020-04-04
    • 2021-05-19
    • 2016-08-30
    • 2021-03-27
    • 2020-04-14
    • 1970-01-01
    相关资源
    最近更新 更多