在Pandas Lambda函数中使用Apply与多个if语句 [英] Using Apply in Pandas Lambda functions with multiple if statements

查看:1673
本文介绍了在Pandas Lambda函数中使用Apply与多个if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据这样的数据框中人的大小来推断分类:

I'm trying to infer a classification according to the size of a person in a dataframe like this one:

      Size
1     80000
2     8000000
3     8000000000
...

我希望它看起来像这样:

I want it to look like this:

      Size        Classification
1     80000       <1m
2     8000000     1-10m
3     8000000000  >1bi
...

我知道理想的过程是应用这样的lambda函数:

I understand that the ideal process would be to apply a lambda function like this:

df['Classification']=df['Size'].apply(lambda x: "<1m" if x<1000000 else "1-10m" if 1000000<x<10000000 else ...)

我检查了一些有关lambda函数中多个if的帖子,此处是一个示例链接,但是由于某种原因,该synthax在多个ifs语句中对我不起作用,但在单个if条件中起作用.

I checked a few posts regarding multiple ifs in a lambda function, here is an example link, but that synthax is not working for me for some reason in a multiple ifs statement, but it was working in a single if condition.

所以我尝试了这种非常优雅"的解决方案:

So I tried this "very elegant" solution:

df['Classification']=df['Size'].apply(lambda x: "<1m" if x<1000000 else pass)
df['Classification']=df['Size'].apply(lambda x: "1-10m" if 1000000 < x < 10000000 else pass)
df['Classification']=df['Size'].apply(lambda x: "10-50m" if 10000000 < x < 50000000 else pass)
df['Classification']=df['Size'].apply(lambda x: "50-100m" if 50000000 < x < 100000000 else pass)
df['Classification']=df['Size'].apply(lambda x: "100-500m" if 100000000 < x < 500000000 else pass)
df['Classification']=df['Size'].apply(lambda x: "500m-1bi" if 500000000 < x < 1000000000 else pass)
df['Classification']=df['Size'].apply(lambda x: ">1bi" if 1000000000 < x else pass)

计算出通过"似乎也不适用于lambda函数:

Works out that "pass" seems not to apply to lambda functions as well:

df['Classification']=df['Size'].apply(lambda x: "<1m" if x<1000000 else pass)
SyntaxError: invalid syntax

在Pandas的apply方法中,关于lambda函数内部的多个if语句的正确Synthax有何建议?多行或单行解决方案都可以为我工作.

Any suggestions on the correct synthax for a multiple if statement inside a lambda function in an apply method in Pandas? Either multi-line or single line solutions work for me.

推荐答案

下面是一个可以建立的小示例:

Here is a small example that you can build upon:

基本上,lambda x: x..是函数的短形式.应用程序真正需要的是一个可以轻松地重新创建自己的函数.

Basically, lambda x: x.. is the short one-liner of a function. What apply really asks for is a function which you can easily recreate yourself.

import pandas as pd

# Recreate the dataframe
data = dict(Size=[80000,8000000,800000000])
df = pd.DataFrame(data)

# Create a function that returns desired values
# You only need to check upper bound as the next elif-statement will catch the value
def func(x):
    if x < 1e6:
        return "<1m"
    elif x < 1e7:
        return "1-10m"
    elif x < 5e7:
        return "10-50m"
    else:
        return 'N/A'
    # Add elif statements....

df['Classification'] = df['Size'].apply(func)

print(df)

返回:

        Size Classification
0      80000            <1m
1    8000000          1-10m
2  800000000            N/A

这篇关于在Pandas Lambda函数中使用Apply与多个if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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