Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Applying function to columns in DataFrame

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading