【发布时间】:2021-06-04 11:52:49
【问题描述】:
目标:定义函数以使用标志 (1,2,3) 作为触发不同权重 (.2,.4,0) 的条件。输出是一个只有权重的新 df。
np.select 产生了这个错误:
TypeError: 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, 2, 3],
'flag_2': [1, 1, 1],
'flag_3': [2, 1, 2],
'flag_4': [3, 1, 3],
'flag_5' : [1, 2, 2],
'flag_6': [2, 1, 2],
'flag_7': [1, 1, 1],
'flag_8': [1, 1, 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,2,3]
choices = [wt1,wt2,wt3]
dfout=np.select(conditions, choices, default=np.nan)
return(dfout.iloc[:,::-1])
inc_weights = inc_weights(flags, .2, .4, 0)
print(inc_weights)
【问题讨论】:
-
为了让我们帮助您,您必须展示您的努力并提交数据以用于重现您的问题。虽然提供图像很有帮助,但它不允许重现问题。请编辑您的问题以显示最小的可重现集。详情请见Minimal Reproducible Example。
-
数据在 flags 数据框中,与图像中的输入完全对应。
-
@itprorh66 我已经查看了最小的 reprex 规则,并试图尽可能地加强它。感谢您的反馈。对于这一步,只是尝试让 np.select 仅适用于增量权重。
-
您能否将您输入的示例发布为代码而不是图像。如果您将预期的输出作为代码发布也会有所帮助,但我不认为这与输入一样重要。
-
代码中的flags数据框是输入。
标签: python-3.x pandas loops cumulative-sum