将二进制编码转换为类multilabel python [英] Converting binary encoding to classes multilabel python

查看:56
本文介绍了将二进制编码转换为类multilabel python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have data like this in dataframe

<身体>
文字 Label1 Label2 label3 label4 标签
你好,我叫约翰 1 0 1 0

我想基于1和0填充标签列,就像这样

and I want to fill the labels column based on the ones and zeros to be like this

<身体>
文字 Label1 Label2 label3 label4 标签
你好,我叫约翰 1 0 1 0 ['Label1','Label3']

我该如何在python中做什么?

How can I do in python?

推荐答案

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

Ok, this is an untested suggestion from this answer. Try gathering the appropriate column labels then using DataFrame.apply on a suitable function:

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)

正如我所说,这是未经检验的;可能需要进行代码调整.

As I say this is untested; there may be code tweaks required.

这篇关于将二进制编码转换为类multilabel python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆