Reputation: 71
I'm new to python and I have a data frame that looks like this:
d = {'1-1.a': ["Yes", ""], '1-1.b': ["No", ""],'1-1.c':["Yes","Yes"],'1-1.d':["",""],'2-1.a':["Yes","No"]}
d = pd.DataFrame(data=d)
and I want to create a new column called Q1 where it's an aggregate of columns 1-1.a-d and looks like the following:
e = {'1-1.a': ["Yes", ""], '1-1.b': ["No", ""],'1-1.c':["Yes","Yes"],'1-1.d':["",""],'1-Q1':["a,c","c"],'2-1.a':["Yes","No"]}
e = pd.DataFrame(data=e)
Upvotes: 0
Views: 61
Reputation: 323396
We can try dot
d['Q1'] = d.eq('Yes').dot(d.columns.str.split('.').str[1]+',').str[:-1]
Out[159]:
0 a,c
1 c
dtype: object
Upvotes: 1