Home > database >  Applying function to columns in DataFrame
Applying function to columns in DataFrame

Time:11-20

I have a DataFrame with columns ['A', 'B', 'C']. I am trying to normalize each of the column using my function. The problem is that it works when I do normalization(df['A']), but doesn't work when I pass a list to the function:

def normalization(x):
    x = (x - np.min(x)) / (np.max(x) - np.min(x))

for column in df.columns:
    normalization(df[column])

How to deal with it in this case? I did read answers with .map and .apply but that didn't work in my case for some reason. I am new to Python, hope my question makes sense.

CodePudding user response:

The problem is your normalization function. it should return the value of the normalization:

def normalization(x):
    return (x - np.min(x)) / (np.max(x) - np.min(x))

When you don't return the value the value None is returned causing the values in map\apply to be None.

Example of working code:

import pandas as pd
import numpy as np

def normalization(x):
    return (x - np.min(x)) / (np.max(x) - np.min(x))

data = {'A': [1, 2, 3], 'B': [3, 4, 5], 'C': [4,5,6]}
df = pd.DataFrame(data=data)
df = df.apply(normalization)
  • Related