gpt4 book ai didi

python - 如果 Pandas 从多列返回值等于另一列中的值

转载 作者:行者123 更新时间:2023-12-01 19:24:30 28 4
gpt4 key购买 nike

我有一个像这样的 Pandas 数据框:

  A      B        C         D
0 month month+1 quarter+1 season+1
1 season month+5 quarter+3 season+2
2 day month+1 quarter+2 season+1
3 year month+3 quarter+4 season+2
4 quarter month+2 quarter+1 season+1
5 month month+4 quarter+1 season+2

我想根据几个 IF 条件插入一个名为“E”的新列。如果“A”列等于“月”,则返回“B”中的值,如果“A”列等于“季度”,则返回“C”中的值,如果“A”列等于“季节”,则返回“D”中的值,如果不是,则返回“A”列中的值

  A      B        C         D        E
0 month month+1 quarter+1 season+1 month+1
1 season month+5 quarter+3 season+2 season+2
2 day month+1 quarter+2 season+1 day
3 year month+3 quarter+4 season+2 year
4 quarter month+2 quarter+1 season+1 quarter+1
5 month month+4 quarter+1 season+2 month+4

我在做这件事时遇到了麻烦。我尝试过使用一个函数,但它不起作用。看我的尝试:

def f(row):
if row['A'] == 'month':
val = ['B']
elif row['A'] == 'quarter':
val = ['C']
elif row['A'] == 'season':
val = ['D']
else:
val = ['A']
return val

df['E'] = df.apply(f, axis=1)

已编辑:将最后一个else更改为“A”列

最佳答案

首先,我建议您查看:When should I want to use apply() in my code.

我会使用Series.replace

df['E'] = df['A'].replace(['month','quarter','season'],
[df['B'], df['C'], df['D']])

numpy.select

cond = [df['A'].eq('month'), df['A'].eq('quarter'), df['A'].eq('season')]
values= [df['B'], df['C'], df['D']]
df['E']=np.select(cond,values,default=df['A'])
<小时/>
  A      B        C         D        E
0 month month+1 quarter+1 season+1 month+1
1 season month+5 quarter+3 season+2 season+2
2 day month+1 quarter+2 season+1 day
3 year month+3 quarter+4 season+2 year
4 quarter month+2 quarter+1 season+1 quarter+1
5 month month+4 quarter+1 season+2 month+4

关于python - 如果 Pandas 从多列返回值等于另一列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732002/

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