Python TypeError:str,int,float

时间:2018-05-02 18:20:26

标签: python types

我正在接受

TypeError: 'float' object is not subscriptable 

为下面的最后一行。我不懂为什么。这是错误的值还是指数?

假设这是因为值,我尝试将这些值转换为floatstr,但这两种方法都没有修复此错误,而是我得到int或str类型的TypeErrors,具体取决于方式我投了他们。

我在StackOverflow上用其相同的描述查看了其他几个问题,但这些都没有解决我的问题。有人可以帮忙吗?我清楚了吗?

def initialize_combined_scores(self):
    sum_weights = self.dist_weight + self.coc_weight + self.optimization_weight + self.productivity_weight
    normalized_dist_weight = self.dist_weight / sum_weights
    normalized_coc_weight = self.coc_weight / sum_weights
    normalized_optimization_weight = self.optimization_weight / sum_weights
    normalized_productivity_weight = self.productivity_weight / sum_weights
    self.combined_scores = normalized_dist_weight
    self.combined_scores_df = pandas.DataFrame({'WorkerID':self.worker_ids})
    self.combined_scores_df['WorkerClusterLat'] = [xx for xx,yy in self.worker_ll]
    self.combined_scores_df['WorkerClusterLon'] = [yy for xx,yy in self.worker_ll]
    for col_name in self.worker_col_keepers:
        self.combined_scores_df[col_name] = self.worker_df[col_name]

    for pidx, patient_id in enumerate(self.patient_ids):
        self.combined_scores_df[str(patient_id)] = self.combined_scores[:, pidx]

1 个答案:

答案 0 :(得分:1)

'-'可能是self.combined_scores。我在你的代码中看到以下几行:

float

这些名称表明,normalized_dist_weight = self.dist_weight / sum_weights ... self.combined_scores = normalized_dist_weight self.dist_weight都可以sum_weights。除法的结果将是float

修改

如果float是多维容器,则最后一行只能起作用。可能是self.combined_scores

我认为错误在:

pandas.DataFrame

这里似乎缺少一些东西。类似名称的self.combined_scores = normalized_dist_weight 绑定到下面一行中的self.combined_scores_df。我相信,创建pandas.DataFrame并将其绑定到pandas.DataFrame的行被错误地删除了。