Нормализация в пандах через функциюPython

Программы на Python
Anonymous
Нормализация в пандах через функцию

Сообщение Anonymous »

У меня есть этот фрейм данных:

Код: Выделить всё

    age
0   48
1   7
2   62
3   48
4   51
Этот код:

Код: Выделить всё

import pandas as pd
import numpy as np

def normalizar(x):
# Convert x to a numpy array to allow for vectorized operations.
x = np.array(x)
# Calculate the minimum and maximum values of x.
xmin = x.min()
xmax = x.max()
# Normalize the array x using vectorized operations.
return (x - xmin) / (xmax - xmin)

df["age_n"] = df["age"].apply(normalizar)
df
и я получаю:

Код: Выделить всё

   age  age_n
0   48    NaN
1    7    NaN
2   62    NaN
3   48    NaN
4   51    NaN
Как решить эту проблему?
Ожидаемым результатом будут значения между [0,1]

Подробнее здесь: https://stackoverflow.com/questions/790 ... a-function

Вернуться в «Python»