Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

python - How to use apply function to normalize columns pandas

I need to normalize my columns using the formula:

x/total * 100

I created my custom function here:

def normalize(df):
    result=[]
    total= df[col].sum()
    for value in df[col]:
        result.append(value/total *100)
    return result

applying the function here. I get TypeError: normalize() got an unexpected keyword argument 'axis'

df['columnname'].apply(normalize, axis=1)

Please how could I do this? Or/and is there a more efficient way of doing this? Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to change normalize to -

def normalize(inp):
    inp = inp.values
    result=[]
    
    total= sum(inp)
    for value in inp:
        result.append(value/total *100)
    return result

#### Using Numpy Directly , avoiding the for loop
def normalize_numpy(inp):
    total= sum(inp)
    return inp/total * 100

>>> l = [100,34,56,71,2,4,5,2,10]
>>> v = [1,2,3,4,5,68,1,2,3]

>>> df = pd.DataFrame(data=list(zip(l,v)),columns=['Col1','Col2'])

Multiple Column Usage

>>> df[['Col1','Col2']].apply(lambda x:normalize_numpy(x),axis=0)
        Col1       Col2
0  35.211268   1.123596
1  11.971831   2.247191
2  19.718310   3.370787
3  25.000000   4.494382
4   0.704225   5.617978
5   1.408451  76.404494
6   1.760563   1.123596
7   0.704225   2.247191
8   3.521127   3.370787

Single Column Usage

>>> df[['Col2']].apply(normalize_numpy,axis=0)
       Value
0   1.123596
1   2.247191
2   3.370787
3   4.494382
4   5.617978
5  76.404494
6   1.123596
7   2.247191
8   3.370787

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...