基于 np.select 条件生成增量权重的函数

问题描述

目标:定义函数以使用标志 (1,2,3) 作为触发不同权重 (.2,.4,0) 的条件。输出是一个只有权重的新 df。

np.select 正在生成此错误:

类型错误:condlist 中的无效条目 0:应该是布尔值 ndarray

图像将所需输出显示为“增量权重输出”

import pandas as pd
import numpy as np

flags = pd.DataFrame({'Date': ['2020-01-01','2020-02-01','2020-03-01'],'flag_1': [1,3],'flag_2': [1,1,1],'flag_3': [2,2],'flag_4': [3,'flag_5' : [1,'flag_6': [2,'flag_7': [1,'flag_8': [1,'flag_9': [3,3,2]})

flags = flags.set_index('Date')

def inc_weights(dfin,wt1,wt2,wt3):
    dfin = pd.DataFrame(dfin.iloc[:,::-1])
    dfout = pd.DataFrame()
    conditions = [1,3]
    choices = [wt1,wt3]
    dfout=np.select(conditions,choices,default=np.nan)
    return(dfout.iloc[:,::-1])
inc_weights = inc_weights(flags,.2,0)
print(inc_weights)

Input and Output

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)