Using Apply in Pandas Lambda functions with multiple if statements

后端 未结 4 1600
一生所求
一生所求 2021-02-09 12:08

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
...
<         


        
4条回答
  •  無奈伤痛
    2021-02-09 12:49

    The apply lambda function actually does the job here, just interested what the problem was.... as your syntax looks ok and it works....

    df1= [80000, 8000000, 8000000000, 800000000000]
    df=pd.DataFrame(df1)
    df.columns=['size']
    df['Classification']=df['size'].apply(lambda x: '<1m' if x<1000000  else '1-10m' if 1000000

    Output:

提交回复
热议问题