Manu Sharma
Manu Sharma

Reputation: 1729

Pandas column: applying a function

I have a pandas data frame df

df:

GROUP VALUE
 1     5
 2     2
 1     10
 2     20
 1     7

And I am trying to apply the following function on one of the column

import pandas as pd
from statsmodels import robust
import numpy as np

def madout(x):
    mad = robust.mad(x)
    median = np.median(x)
    mad_s = (abs(x - median / mad))
    return mad_s

df.VALUE.apply(madout)

but receiving an error despite my several attempts

AxisError: axis 0 is out of bounds for array of dimension 0. Please help

Upvotes: 1

Views: 53

Answers (1)

pberkes
pberkes

Reputation: 5360

apply is going to apply the function to every element of the column df.VALUE.

I think what you are looking for is:

In [8]: madout(df.VALUE)
Out[8]: 
0     3.426191
1     0.426191
2     8.426191
3    18.426191
4     5.426191
Name: VALUE, dtype: float64

Upvotes: 2

Related Questions