使用 np.where 创建列时出现问题,ArrayType 错误
Issue with creating a column using np.where, ArrayType error
我有一个数据框,当满足某些条件时,我试图在其中创建二进制 1/0 列。我使用的代码如下:
sd_threshold = 5
df1["signal"] = np.where(np.logical_and(df1["high"] >= df1["break"], df1["low"]
<= df1["break"], df1["sd_round"] > sd_threshold), 1, 0)
代码returns TypeError: return arrays must be of ArrayType
当包含最后一个条件df1["sd_round"] > sd_threshold
时,否则可以正常工作。 df1["sd_round"] 列中的数据没有任何问题。
任何见解将不胜感激,谢谢!
check the documentation -- np.logical_and()
compares the first two arguments you give it and writes the output to the third. you could use a nested call but i would just go with &
(pandas boolean indexing):
df1["signal"] = np.where((df1["high"] >= df1["break"]) &
(df1["low"] <= df1["break"]) &
(df1["sd_round"] > sd_threshold),
1, 0)
编辑:您实际上可以跳过 numpy 并将布尔系列转换为 int 以产生 1 和 0:
mask = ((df1["high"] >= df1["break"]) &
(df1["low"] <= df1["break"]) &
(df1["sd_round"] > sd_threshold))
df1["signal"] = mask.astype(int)
我有一个数据框,当满足某些条件时,我试图在其中创建二进制 1/0 列。我使用的代码如下:
sd_threshold = 5
df1["signal"] = np.where(np.logical_and(df1["high"] >= df1["break"], df1["low"]
<= df1["break"], df1["sd_round"] > sd_threshold), 1, 0)
代码returns TypeError: return arrays must be of ArrayType
当包含最后一个条件df1["sd_round"] > sd_threshold
时,否则可以正常工作。 df1["sd_round"] 列中的数据没有任何问题。
任何见解将不胜感激,谢谢!
check the documentation -- np.logical_and()
compares the first two arguments you give it and writes the output to the third. you could use a nested call but i would just go with &
(pandas boolean indexing):
df1["signal"] = np.where((df1["high"] >= df1["break"]) &
(df1["low"] <= df1["break"]) &
(df1["sd_round"] > sd_threshold),
1, 0)
编辑:您实际上可以跳过 numpy 并将布尔系列转换为 int 以产生 1 和 0:
mask = ((df1["high"] >= df1["break"]) &
(df1["low"] <= df1["break"]) &
(df1["sd_round"] > sd_threshold))
df1["signal"] = mask.astype(int)