Saro
Saro

Reputation: 107

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.

Upvotes: 0

Views: 141

Answers (1)

Nils Werner
Nils Werner

Reputation: 36765

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())

Upvotes: 1

Related Questions