python使用多个条件创建一个新列

2024-03-29 08:03:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚从Python开始,我有一个很大的受试者列表和他们的(BMI)体重指数(以及更多的数据)。 我需要创建一个新的列(称为OMS),在那里我可以说明他们是否“正常”、“超重”、“肥胖”,等等。你知道吗

但我就是找不到正确的方法。我试过了np.什么时候但这只适用于两个条件。你知道吗

我尝试了if,elif,else但没有成功,还有:

df['oms'] = np.nan

df['oms'].loc[(df['IMC'] <=18.5 )] = "slim"

df['oms'].loc[(df['IMC'] >= 18.5) & (df['IMC'] <25 )] = "normal"

df['oms'].loc[(df['IMC'] >= 25) & (df['IMC'] <=30 )] = "overweight"

df['oms'].loc[(df['IMC'] > 30)] = "obese"

有什么想法吗?我卡住了。你知道吗


Tags: 数据方法df列表ifnp指数条件
3条回答
df.loc[df['IMC'].lt(18.5), 'oms'] = "slim"
df.loc[df['IMC'].ge(18.5) & df['IMC'].lt(25), 'oms'] = "normal"
df.loc[df['IMC'].ge(25) & df['IMC'].lt(30), 'oms'] = "overweight"
df.loc[df['IMC'].ge(30), 'oms'] = "obese"

也可以使用^{}。你知道吗

bins = [0, 18.5, 25, 30, 9999]
labels = ['slim', 'normal', 'overweight', 'obese']

df = pd.DataFrame({'IMC': [15, 20, 27, 40]})
df['oms'] = pd.cut(df['IMC'], bins, labels=labels)
>>> df
   IMC         oms
0   15        slim
1   20      normal
2   27  overweight
3   40       obese

尝试一下:

df['oms'] = ""#keep it object dtype

df.loc[(df['IMC'] <=18.5 ), 'oms'] = "slim"
df.loc[(df['IMC'] >= 18.5) & (df['IMC'] <25 ), 'oms'] = "normal"
df.loc[(df['IMC'] >= 25) & (df['IMC'] <=30 ), 'oms'] = "overweight"
df.loc[(df['IMC'] > 30), 'oms'] = "obese"

使用numpy.select,我喜欢这个替代方法,因为它非常通用,您可以轻松地添加或删除条件。你知道吗

import numpy as np

condlist = [df["IMC"] <= 18,
           (df["IMC"] >= 18.5) & (df['IMC'] <25),
           (df["IMC"] >= 25) & (df['IMC'] <=30),
            df["IMC"] > 30]

condchoice = ["slim", "normal", "overweight", "obese"]

df["oms"] = np.select(condlist, condchoice)

相关问题 更多 >