[英]How to get the column name for a specific values in every row of a dataframe
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. 请告知我该如何实现。
EXPECTED OUTPUT: 预期的输出:
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)
You can make a function that can be applied along axis=1
then apply it to the whole DataFrame. 您可以创建一个可以沿axis=1
应用的函数,然后将其应用到整个DataFrame。
def check_if_s_in_row(row):
present = []
for i in len(range(row)):
if row[i] == "S":
present.append(row.columns.values[i])
return ["S present in {}".format(day) for day in present]
dataframe.apply(check_if_s_in_row, axis=1)
Do the appropriate for L. 对L做适当的事。
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
This is only for match your output 这仅用于匹配您的输出
[' '.join(y.reset_index().groupby(x)['index'].apply(','.join).reset_index().apply(' present '.join,1))for x,y in df.iloc[:,2:].iterrows()]
Out[237]:
['L present Wed,Thu S present Mon,Tue,Fri',
'L present Mon,Thu S present Wed,Fri W present Tue']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.