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

问题描述

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

I have data like this in dataframe

你好,我叫约翰 1 0 1 0

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

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

你好,我叫约翰 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:43