使用Lambda函数 pandas 设置列值 [英] Using Lambda Function Pandas to Set Column Values

查看:40
本文介绍了使用Lambda函数 pandas 设置列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以建议使用lambda函数来回答相同问题的方法(请参阅链接): 在逐行迭代的同时更新熊猫中的数据框

Could anyone suggest a way answer the same question (see link) but by using lambda function: Update a dataframe in pandas while iterating row by row

推荐答案

您需要将apply与参数axis=1一起使用,以确保传递给apply的函数应用于每行.

You'll want to use apply with the parameter axis=1 to insure the function passed to apply is applied to each row.

所引用的问题具有使用此循环的答案.

The referenced question has an answer that uses this loop.

for i, row in df.iterrows():
    if <something>:
        row['ifor'] = x
    else:
        row['ifor'] = y

    df.ix[i]['ifor'] = x

要使用具有相同逻辑的lambda

To use a lambda with the same logic

df['ifor'] = df.apply(lambda row: x if something else y, axis=1)

这篇关于使用Lambda函数 pandas 设置列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆