Reputation: 23
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)
Upvotes: 0
Views: 552
Reputation: 75150
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
Upvotes: 1
Reputation: 323396
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']
Upvotes: 0
Reputation: 308
You can make a function that can be applied along axis=1
then apply it to the whole 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.
Upvotes: 1