gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:55:21 30 4
gpt4 key购买 nike

我有一个 df(形状 (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

当我尝试添加新列并根据第一个条件 block 附加值时,它工作得很好。

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: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我不知道如何离开这里。在 SQL 中,我会使用两个简单的 WHERE 语句,但我无法让它在 Python 中工作。

最佳答案

选项 #1:两个 map、一个 isin 和一个 np.where

mtrue  = {20: 268.34, 24: 322.02, 30: 492.65, 40: 536.69}
mfalse = {20: 417.43, 24: 500.91, 30: 626.34, 40: 834.85}
ids = {5105561300, 5105561301, 5105561302, 5105561304}

df['new_value'] = np.where(df['ID'].isin(ids), df['CH'].map(mtrue), df['CH'].map(mfalse))
<小时/>

选项#2:一张 map 和一个zip

mtrue  = {20: 268.34, 24: 322.02, 30: 492.65, 40: 536.69}
mfalse = {20: 417.43, 24: 500.91, 30: 626.34, 40: 834.85}
ids = {5105561300, 5105561301, 5105561302, 5105561304}
m = {
(b, k): v for b, d in zip([True, False], [mtrue, mfalse])
for k, v in d.items()
}

df['new_value'] = [*map(m.get, zip(df['ID'].isin(ids), df['CH']))]

相当于:

以防万一你可以[*map...]

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

关于python - 如何使用多个 if 条件创建 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56281396/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com