简体   繁体   中英

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.

There is no need to do any of the for loops:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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