I am trying to define a function so that it applies to multiple rows at once.
Here's what I'm trying to do.
I have column a, which has information around payments set up by channel by consumers. I am trying to convert it into separate binary columns (flags). For if column a =Source1, then I want to create a column named Source1 which will populate 1, else 0. For column a=Source2, then column named Source2 will populate 1, else 0. So on and so forth. Below is the sample of my code:
def payer(row1,row2,row3):
if file['a'] == 'Source1':
return {row1:1, row2:0, row3:0}
elif file['a'] == 'Source2':
return {row1:0, row2:1, row3:0}
elif file['a'] == 'Sourc3':
return {row1:0, row2:0, row3:1}
else:
return {row1:0, row2:0, row3:0}
file[['Source1','Source2','Source3']] =""
file[['Source1','Source2','Source3']]= file[['Source1','Source2','Source3']].apply(lambda a:(a), axis=0)
I'm absolutely new, but have been searching for a while, but can't find any solution. Please help!
Output