用于标记数据的 Numpy 逻辑条件

Numpy logical conditions for labeling the data

我正在尝试创建另一个基于现有数据中多个条件的标签列

df

ind group people value value_50 val_minmax
 1     1    5    100    1        10
 1     2    2    90     1        na
 2     1    10   80     1        80
 2     2    20   40     0        na
 3     1    7    10     0        10
 3     2    23   30     0        na

import pandas as pd 
import numpy as np 

df = pd.read_clipboard()

然后尝试根据以下条件在行上放置标签

df['label'] = np.where(np.logical_and(df.group == 2, df.value_50 == 1, df.value > 50), 1, 0)

但它给我一个错误

TypeError: return arrays must be of ArrayType

如何在python中执行?

在掩码之间使用 &

df['label'] = np.where((df.group == 2) & (df.value_50 == 1) & (df.value > 50), 1, 0)

选择:

df['label'] = ((df.group == 2) & (df.value_50 == 1) & (df.value > 50)).astype(int)

如果将 reduce 与布尔掩码列表一起使用,您的解决方案应该有效:

mask = np.logical_and.reduce([df.group == 2, df.value_50 == 1, df.value > 50])
df['label'] = np.where(mask, 1, 0)
#alternative
#df['label'] = mask.astype(int)