Python 如何使用多个if条件创建for循环

Python 如何使用多个if条件创建for循环,python,pandas,if-statement,conditional-statements,Python,Pandas,If Statement,Conditional Statements,我有一个df(shape(5928,22)),我正在尝试创建一个新列,并根据多个条件添加值 条件是: if CH == 20 then value = 268,34 if CH == 24 then value = 322,02 if CH == 30 then value = 492,65 if CH == 40 then value = 536,69 and if CH == 20 & ID in (5105561300, 5105

我有一个df(shape(5928,22)),我正在尝试创建一个新列,并根据多个条件添加值

条件是:

    if CH == 20 then value = 268,34
    if CH == 24 then value = 322,02
    if CH == 30 then value = 492,65
    if CH == 40 then value = 536,69

    and

    if CH == 20 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 417,43
    if CH == 24 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 500,91
    if CH == 30 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 626,34
    if CH == 40 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 834,85
当我尝试添加一个新列并根据第一个条件块附加值时,它工作得非常好

new_value = []

for row in df['CH']:
    if row == 20:
        new_value.append(268.34)
    elif row == 24:
        new_value.append(322.02)
    elif row == 30:
        new_value.append(402.65)
    elif row == 40:
        new_value.append(536.69)
    else:
        new_value.append(0)

df['new_value'] = new_value
当我尝试添加其他条件时,它不会起作用。代码类似于:

new_value = []

for row in df['CH']:
    if row == 20 and df['ID'] not in (5105561300, 5105561301, 5105561302, 5105561304):
         new_value.append(268.34)
    elif row == 20 and df['ID'] in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(417.43)
    elif row == 24 and df['ID'] not in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(268.34)
    elif row == 24 and df['ID'] in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(500.91)
    elif row == 30 and df['ID'] not in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(268.34)
    elif row == 30 and df['ID'] in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(626.34)
    elif row == 40 and df['ID'] not in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(268.34)
    elif row == 40 and df['ID'] in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(834.85)
    else:
        new_value.append(0)

    df['new_value'] = new_value
当我尝试上面的代码时,会收到以下错误消息:

ValueError:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()


我不知道从这里该怎么走。在SQL中,我将使用两个简单的WHERE语句,但在Python中无法使用

您似乎可以将这一点整合起来,并避免冗余:

default = 268.34

for row in df['CH']:
    id_check = df['ID'] in (5105561300, 5105561301, 5105561302, 5105561304)
    if row == 20:
        new_value = 417.43
    elif row == 24:
        new_value = 500.91
    elif row == 30:
        new_value = 626.34
    elif row == 40
        new_value = 834.85
    else:
        new_value = 0
    df['new_value'] = default if not id_check else value
或者,您可以将其映射为:

def get_new_value(row):
    d = { 20: 417.43,
             24: 500.91,
             30: 626.34,
             40: 834.85 }
    return d.get(row, 0)

default = 268.34
for row in df['CH']:
    id_check = df['ID'] in (5105561300, 5105561301, 5105561302, 5105561304)
    new_value = default if not id_check else get_new_value(row)

    df['new_value'] = new_value
选项1:两个
map
,一个
isin
,一个
np.where

选项2:一张
地图
和一张
邮政编码
相当于: 以防万一,您可以执行
[*映射…]

df['new_value'] = [m[t] for t in zip(df['ID'].isin(ids), df['CH']))]

代码的问题在
df['ID']
中,请将行循环方式更改为以下内容,以修复错误消息:

for row, id in zip(df['CH'], df['ID']):
    if row == 20 and id not in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(268.34)
    elif row == 20 and id in (5105561300, 5105561301, 5105561302, 5105561304):
        ...
由于数据集不是很大,您可以使用列表理解来处理此任务:

# a set of ids to check existence
wlist = { 5105561300, 5105561301, 5105561302, 5105561304 }

# the value of each key is a list with the first element using the value 
# when id not in wlist and the 2nd element the value when id is in wlist
mapping = {
    20: [268.34, 417.43]
  , 24: [322.02, 500.91]
  , 30: [492.65, 626.34]
  , 40: [536.69, 834.85]
}

# new_value will depend on if CH is in mapping and id in wlist
df['new_value'] = [ mapping[ch][int(id in wlist)] if ch in mapping else 0 for ch, id in zip(df.CH, df.ID) ]

作为将来的参考,
dict
set
可能有助于研究。我可以用这些来回答这个问题,但我相信有一个更好的解决方案,所以我让这些向导中的一个来处理。请在循环中执行一个print语句:print(row==20)。我们需要看看这是否是一个布尔值。它可能正在执行逐点检查,因此是一个系列…您有一个bug,它将返回bool的行检查(row==40)与返回系列的列检查(df['ID']不在((51055613005105561301))。尝试更多链接(df.CH==20)和(df.ID.isin(51055613005105561301 51055613025105561304)==False),用于矢量化观察效果很好,Peter!当我尝试你的建议时,我得到了#TypeError:isin()接受2个位置参数,但为回复提供了5个参数!没有默认值。该值根据两个条件(CH和ID)更改1.也许我不明白你的意思suggestion@cebs在操作代码的最后一个片段中,只要
df['ID']
不在该值元组中,就分配相同的值268.34。可以随意调用它,但它看起来像是“默认值”对我来说,这就是为什么我这样命名它。也许这是一个输入错误,因为在你的其他代码中,你使用了一些不同的值。你可能想修改你的问题,使其不那么模棱两可。jxc下面的解决方案是类似的,但考虑到你在早期代码中使用的不同值。太棒了!!它成功了!我唯一需要更改的是将mtrue切换到mfalse…非常感谢!!
for row, id in zip(df['CH'], df['ID']):
    if row == 20 and id not in (5105561300, 5105561301, 5105561302, 5105561304):
        new_value.append(268.34)
    elif row == 20 and id in (5105561300, 5105561301, 5105561302, 5105561304):
        ...
# a set of ids to check existence
wlist = { 5105561300, 5105561301, 5105561302, 5105561304 }

# the value of each key is a list with the first element using the value 
# when id not in wlist and the 2nd element the value when id is in wlist
mapping = {
    20: [268.34, 417.43]
  , 24: [322.02, 500.91]
  , 30: [492.65, 626.34]
  , 40: [536.69, 834.85]
}

# new_value will depend on if CH is in mapping and id in wlist
df['new_value'] = [ mapping[ch][int(id in wlist)] if ch in mapping else 0 for ch, id in zip(df.CH, df.ID) ]