【问题标题】:Converting binary encoding to classes multilabel python将二进制编码转换为类多标签python
【发布时间】:2021-04-22 15:43:46
【问题描述】:

我在数据框中有这样的数据

Text Label1 Label2 label3 label4 Labels
Hello my name is john 1 0 1 0

我想根据 1 和 0 来填充标签列,就像这样

Text Label1 Label2 label3 label4 Labels
Hello my name is john 1 0 1 0 ['Label1','Label3']

在python中我该怎么做?

【问题讨论】:

  • 值已准备就绪,没有公式可供选择
  • 我在想 ['Label1', 'Label2'] 是一个错字,应该是 ['Label1', 'Label3' ],你能确认吗?
  • 是的,抱歉,这是一个错字.. 已编辑。谢谢

标签: python dataframe machine-learning


【解决方案1】:

好的,这是来自this answer 的未经测试的建议。尝试收集适当的列标签,然后在合适的函数上使用 DataFrame.apply:

test_cols = [c for c in df.columns if c[:5].lower() == 'label']
test_cols.remove('Labels')

def aggLabels(aSeries):
    return [lab for lab in test_cols if aSeries[lab]==1]

df['Labels'] = df.apply(aggLabels, axis=1)

正如我所说,这是未经测试的;可能需要对代码进行调整。

【讨论】:

    猜你喜欢
    • 2020-10-04
    • 2020-03-12
    • 2021-03-18
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2016-08-13
    相关资源
    最近更新 更多