【问题标题】:Getting AttributeError: 'str' object has no attribute loc获取 AttributeError:'str' 对象没有属性 loc
【发布时间】:2020-12-18 02:03:08
【问题描述】:
infections = []

for i in range(0, 20):
    read_nii(raw_data.loc[i,'infection_mask'], infections, 'infections')

在执行此代码时,我收到 AttributeError: 'str' object has no attribute 'loc'。如何解决此错误?

【问题讨论】:

  • 你能分享raw_data是如何定义/初始化的吗?

标签: python pandas attributeerror pandas-loc


【解决方案1】:

看来:

  1. 您有一个名为 raw_datastring 变量。

  2. 从这个变量中,您创建了一个 DataFrame(例如,命名为 df)。

  3. 您尝试检索一些数据(来自 infection_mask 列, 从前 20 行开始)。

  4. 但您不是从 df 中检索这些数据,而是尝试 从保存原始数据(字符串)的变量中检索它们。

其他可能的情况是raw_data前一段时间是一个DataFrame, 但由于您的代码中有一些错误,您已经用一些 text 覆盖了它 内容。

在违规指令前添加print(type(raw_data))。 结果可能是 str(一个字符串)。

然后分析写入raw_data变量的每种情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多