【发布时间】:2020-09-18 18:50:49
【问题描述】:
下面是一种热编码的实现,每列下有多个类别。
Ttop_10_LMR = [x for x in tdata.Loan_Amount_Requested.value_counts().sort_values(ascending=False).head(10).index]
Ttop_10_LMR 变量给出 Loan_Amount_Requested 列中的前 10 个频繁值。
def one_hot_top_x(df, variable, top_x_labels):
for label in top_x_labels:
df[variable+'_'+str(label)] = np.where(data[variable]==label, 1, 0)
one_hot_top_x func 将通过将 top_x_labels 替换为 1 和 0 来向 tdata Dataframe 添加新列。
但是当我运行下面的代码时,
one_hot_top_x(tdata, 'Loan_Amount_Requested', Ttop_10_LMR)
获取为 ValueError:值的长度与索引的长度不匹配
谢谢。
【问题讨论】:
标签: python pandas dataframe jupyter-notebook valueerror