0

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)
petezurich
  • 9,280
  • 9
  • 43
  • 57
Jagdish693
  • 23
  • 1
  • 3
  • 1
    I'm very confused about what you expect. I have a hard time believing you actually want to build a string that looks like `'(S present in Wed,Fri L present in Mon,Thu)'`. And if you do, then that is all the information that is basically in the dataframe already. Why would you want a string version of the row in another column. Anyway, I'm confused. – piRSquared May 17 '19 at 15:47

3 Answers3

1

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.

Rhubarb
  • 198
  • 1
  • 11
  • I think the desired output doesn't match. The author wants to evaluate multiple values at the same time. Plus why would one re-write the function for each value instead of passing the value as a variable to the function? – Julia May 17 '19 at 15:53
1

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
anky
  • 74,114
  • 11
  • 41
  • 70
  • Thanks @anky_91 , Can you please suggest how can i achieve that text format for that list in the _present column Ex: S present in Mon,Tue,Fri – Jagdish693 May 18 '19 at 13:34
0

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']
BENY
  • 317,841
  • 20
  • 164
  • 234