将定义的函数应用于Python上的多行

发布于 2025-01-24 10:34:05 字数 678 浏览 6 评论 0原文

我正在尝试定义一个函数,以便一次适用于多行。

这是我想做的。

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2025-01-31 10:34:05
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)

输出

            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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文