python pandas dataframe单元格更新错误

标签 python pandas numpy machine-learning

我有一个数据框。当尝试更新单元格时,所有更新的单元格值均为零。这是我的代码:

for column in data:

    if column != "id" and column != "diagnosis":
        # change the dtype to 'float64' 
        data[column] = data[column].astype("float")
        columnArray = data[column].values 
        column_max = max(columnArray)
        column_min = min(columnArray)
        print(column_max, " ", column_min,column)
        for index in range(columnArray.shape[0]):
            cell_value = columnArray[index]
            new_value = (cell_value-column_min)/(column_max-column_min) 
            # print(new_value)
            data.at[index,column] = new_value

我还应该提到,我对 pandas 和 NumPy 有点陌生,可能有一个内置函数可以毫无痛苦地标准化我的功能。

最佳答案

不需要执行任何 for 循环:

columns = ~data.columns.isin(['id', 'diagnosis'])
data.loc[:, columns] = (data.loc[:, columns] - data.loc[:, columns].min()) / (data.loc[:, columns].max() - data.loc[:, columns].min())

关于python pandas dataframe单元格更新错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899800/

相关文章:

python - 使用 numpy 读/写 Fortran 顺序数组的正确方法

python - 在 numba 中实现的 tensordot 算法比 numpy 慢得多

python - 如何从 homebrew、distribute、macports、pip 中卸载所有软件包?

python - 如何在 Google App Engine Datastore 中存储非 ASCII 字符

python - “DataFrameGroupBy”对象没有属性 'set_index'

python - 重命名 Pandas 数据框的索引

python - 向量化函数以从日期列表中获取最接近的日期

python - 分解数组数组-(Dataframe)pySpark

python - TensorFlow :ValueError: None values not supported

matplotlib - 如何从 Pandas 数据框中提取日期索引以用作 matplotlib 中的 x 轴