简体   繁体   English

熊猫专栏:应用功能

[英]Pandas column: applying a function

I have a pandas data frame df 我有一个熊猫数据框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 AxisError:轴0超出维度0数组的范围。请帮助

apply is going to apply the function to every element of the column df.VALUE . apply将函数应用于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM