python - 使用 .apply、.applymap、.groupby 转换 Pandas DataFrame 中的异常值

标签 python numpy pandas outliers

我正在尝试将 pandas DataFrame 对象转换为包含基于一些简单阈值的点分类的新对象:

  • 如果点为 NaN,则值转换为 0
  • 如果点为负或 0,则值转换为 1
  • 如果值超出基于整列的特定条件,则转换为 2
  • 值为 3 否则

这是一个非常简单的独立示例:

import pandas as pd
import numpy as np

df=pd.DataFrame({'a':[np.nan,1000000,3,4,5,0,-7,9,10],'b':[2,3,-4,5,6,1000000,7,9,np.nan]})

print(df)

enter image description here

到目前为止创建的转换过程:

#Loop through and find points greater than the mean -- in this simple example, these are the 'outliers'
outliers = pd.DataFrame()
for datapoint in df.columns:
    tempser = pd.DataFrame(df[datapoint][np.abs(df[datapoint]) > (df[datapoint].mean())])
    outliers = pd.merge(outliers, tempser, right_index=True, left_index=True, how='outer')

outliers[outliers.isnull() == False] = 2


#Classify everything else as "3"
df[df > 0] = 3

#Classify negative and zero points as a "1"
df[df <= 0] = 1

#Update with the outliers
df.update(outliers)

#Everything else is a "0"
df.fillna(value=0, inplace=True)

导致:

enter image description here

我试过使用.applymap()和/或 .groupby()为了加快这个过程而没有运气。我在 this answer 中找到了一些指导但是,我仍然不确定当您不在 pandas 列中分组时 .groupby() 有何用处。

最佳答案

这是异常值部分的替代品。在我的计算机上,您的示例数据的速度大约快 5 倍。

>>> pd.DataFrame( np.where( np.abs(df) > df.mean(), 2, df ), columns=df.columns )

    a   b
0 NaN   2
1   2   3
2   3  -4
3   4   5
4   5   6
5   0   2
6  -7   7
7   9   9
8  10 NaN

您也可以使用 apply 来完成它,但它会比 np.where 方法慢(但与您当前正在执行的速度大致相同),但要简单得多。这可能是一个很好的例子,说明为什么在您关心速度时应尽可能避免使用 apply

>>> df[ df.apply( lambda x: abs(x) > x.mean() ) ] = 2

你也可以这样做,它比 apply 快但比 np.where 慢:

>>> mask = np.abs(df) > df.mean()
>>> df[mask] = 2

当然,这些东西并不总是线性扩展,因此请在您的真实数据上对其进行测试,看看比较结果如何。

关于python - 使用 .apply、.applymap、.groupby 转换 Pandas DataFrame 中的异常值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005725/

相关文章:

Python矩阵操作

python - 将字典中的多个值(每个键)映射并附加到数据帧的不同列

python - 在带有 django-rest-framework 的过滤器中使用自定义方法

python - 如何从 ubuntu 服务器以编程方式登录 Yahoo

Python矢量化嵌套for循环

python - 将自定义累积函数应用于 Pandas 数据框

python - 查找具有 NaN 值的 DataFrame 列表的索引 - Pandas

python - 使用 id() 时 [] 和 list() 有区别吗?

python - 在 Google App Engine 上使用 Django 限制对 Shopify API 的 API 调用速率

Python-根据另一个数据帧匹配替换数据帧中的值