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!
CodePudding user response:
import pandas as pd
def payer(name):
if name == 'IB Plans':
return 1, 2, 3
elif name == 'Web Plans':
return 1, 1, 3
elif name == 'NC Payers':
return 1, 1, 3
else:
return 2, 2, 2
file = pd.DataFrame({'a': ['IB Plans', 'Web Plans', 'NC Payers', 'Some other', 'NC Payers']})
file[['x', 'y', 'z']] = file.apply(lambda row: payer(row['a']), axis=1, result_type='expand')
print(file)
Output
a x y z
0 IB Plans 1 2 3
1 Web Plans 1 1 3
2 NC Payers 1 1 3
3 Some other 2 2 2
4 NC Payers 1 1 3