将定义的函数应用于Python上的多行
我正在尝试定义一个函数,以便一次适用于多行。
这是我想做的。
我有A列,其中包含有关消费者渠道设置的付款的信息。我正在尝试将其转换为单独的二进制列(标志)。对于如果列a = source1,那么我想创建一个名为source1的列,该列将填充1,否则0。对于a = source2,然后列名source2将填充1,else 0。等等。 以下是我的代码的示例:
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 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出
Output