简体   繁体   English

Python Pandas DataFrame单元更新错误

[英]python pandas dataframe cell updating error

I have a dataframe. 我有一个数据框。 When trying to update cells, all updated cell values are zero. 尝试更新单元格时,所有更新的单元格值为零。 This is my code: 这是我的代码:

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

I also should mention that I am a little bit new to pandas and NumPy and that there might be a built-in function that normalizes my features without any pain. 我还应该提到,我对Pandas和NumPy有点陌生,并且可能有一个内置函数可以正常化我的功能而不会带来任何痛苦。

There is no need to do any of the for loops: 无需执行任何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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM