gpt4 book ai didi

python - pandas if else 多列条件

转载 作者:太空宇宙 更新时间:2023-11-04 07:26:57 24 4
gpt4 key购买 nike

<分区>

假设我有以下 df:

import pandas as pd

data_dic = {
"a": [0,0,1,2],
"b": [0,3,4,5],
"c": [6,7,8,9]
}
df = pd.DataFrame(data_dic)

结果:

   a  b  c
0 0 0 6
1 0 3 7
2 1 4 8
3 2 5 9

我需要根据条件将上述列的值传递给新列:

if df.a > 0 then value df.a
else if df.b > 0 then value df.b
else value df.c

现在我尝试:

df['value'] = [x if x > 0 else 'ww' for x in df['a']]

但是不知道怎么在里面输入更多的条件。

预期结果:

   a  b  c value
0 0 0 6 6
1 0 3 7 3
2 1 4 8 1
3 2 5 9 2

谢谢你的辛勤工作。

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