如何获取数据框每一行中特定值的列名 [英] How to get the column name for a specific values in every row of a dataframe

查看:73
本文介绍了如何获取数据框每一行中特定值的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个数据帧中,我想检查当前行中的特定值,如果该值退出,我想获取该值存在于特定行
中的列名,例如: / p>

In a dataframe I want to check for the specific value/'s in the current row and if the value exits i want to get the column name in which the value exists in the partiular row for example:

    Resource  Team  Mon Tue Wed Thu Fri   
19  Name1     Dev   S   S   L   L    S    

11  Name2     QA    L   W   S    L   S    

我希望将输出数据添加到现有框架的新列中。请告知我该如何实现。

i want the output data in a new column to the existing framework. please advise how can i achieve this.

期望的输出:

    Resource     OUTPUT
19  Name1        (S present in Mon,Tue,Fri L present in Wed, Thu)

11  Name2        (S present in Wed,Fri L present in Mon,Thu)


推荐答案

据我了解,您可以执行以下操作:

From what I understand, you can do something like:

m=df.set_index(['Resource','Team'])
m['S_present']=m.eq('S').dot(','+m.columns).str.lstrip(',')
m['L_present']=m.eq('L').dot(','+m.columns).str.lstrip(',')
print(m.reset_index())







  Resource Team Mon Tue Wed Thu Fri    S_present L_present
0    Name1  Dev   S   S   L   L   S  Mon,Tue,Fri   Wed,Thu
1    Name2   QA   L   W   S   L   S      Wed,Fri   Mon,Thu

这篇关于如何获取数据框每一行中特定值的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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