Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标记数据的Numpy逻辑条件_Python_Pandas_Numpy_Dataframe_Where - Fatal编程技术网

Python 标记数据的Numpy逻辑条件

Python 标记数据的Numpy逻辑条件,python,pandas,numpy,dataframe,where,Python,Pandas,Numpy,Dataframe,Where,我正在尝试创建另一个基于现有数据中多个条件的标签列 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

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

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)
如果与布尔掩码列表一起使用,您的解决方案应能正常工作:

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)

在遮罩之间使用
&

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)
如果与布尔掩码列表一起使用,您的解决方案应能正常工作:

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)