【问题标题】:ValueError facing in implementing one hot encoding实现一种热编码时面临的ValueError
【发布时间】: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


    【解决方案1】:
    df[variable+'_'+str(label)] = np.where(tdata[variable]==label, 1, 0)
    

    而不是作为数据给出的 tdata :)

    【讨论】:

      猜你喜欢
      • 2019-02-06
      • 2016-03-19
      • 2019-08-26
      • 2021-12-01
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      相关资源
      最近更新 更多