【问题标题】: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'。如何解决此错误?
【问题讨论】:
标签:
python
pandas
attributeerror
pandas-loc
【解决方案1】:
看来:
-
您有一个名为 raw_data 的 string 变量。
-
从这个变量中,您创建了一个 DataFrame(例如,命名为 df)。
-
您尝试检索一些数据(来自 infection_mask 列,
从前 20 行开始)。
-
但您不是从 df 中检索这些数据,而是尝试
从保存原始数据(字符串)的变量中检索它们。
其他可能的情况是raw_data前一段时间是一个DataFrame,
但由于您的代码中有一些错误,您已经用一些 text 覆盖了它
内容。
在违规指令前添加print(type(raw_data))。
结果可能是 str(一个字符串)。
然后分析写入raw_data变量的每种情况。