gpt4 book ai didi

python - 如何在 Pandas 中进行条件比较

转载 作者:太空宇宙 更新时间:2023-11-03 23:59:35 25 4
gpt4 key购买 nike

我在 Pandas 中有以下数据框

code     prod_a      prod_b     flag
123 MS MS to be checked
123 HS MS more than 1 prod
123 MS HS to be checked
123 HS MS more than 1 prod
123 MS MS to be checked

我只想在 flag = to be checked 和其他标志 more than 1 prod 保持原样的情况下比较 prod_a 和 prod_b。我想要的数据框如下

code     prod_a      prod_b     flag               final_flag
123 MS MS to be checked matched
123 HS MS more than 1 prod more than 1 prod
123 MS HS to be checked not matched
123 HS MS more than 1 prod more than 1 prod
123 MS MS to be checked matched

我怎样才能在 pandas 中做到这一点。


重新创建数据框:

import pandas as pd

data = '''\
code,prod_a,prod_b,flag
123,MS,MS,to be checked
123,HS,MS,more than 1 prod
123,MS,HS,to be checked
123,HS,MS,more than 1 prod
123,MS,MS,to be checked
'''

fileobj = pd.compat.StringIO(data)
df = pd.read_csv(fileobj, sep=',')

最佳答案

使用numpy.select通过 & 为按位 AND 和通过 ~ 反转的链条件:

m1 = df['flag'].eq('to be checked')
m2 = df.prod_a.eq(df.prod_b)

df['final_flag'] = np.select([m1 & m2, m1 & ~m2],['matched','not matched'],default=df['flag'])
print (df)
code prod_a prod_b flag final_flag
0 123 MS MS to be checked matched
1 123 HS MS more than 1 prod more than 1 prod
2 123 MS HS to be checked not matched
3 123 HS MS more than 1 prod more than 1 prod
4 123 MS MS to be checked matched

@Anton vBR 的解决方案:

m1 = df['flag'].eq('to be checked')
m2 = df.prod_a.eq(df.prod_b)

df['final_flag'] = df['flag']
df.loc[m1 & m2, 'final_flag'] = 'matched'
df.loc[m1 & ~m2, 'final_flag'] = 'not matched'
print (df)
code prod_a prod_b flag final_flag
0 123 MS MS to be checked matched
1 123 HS MS more than 1 prod more than 1 prod
2 123 MS HS to be checked not matched
3 123 HS MS more than 1 prod more than 1 prod
4 123 MS MS to be checked matched

关于python - 如何在 Pandas 中进行条件比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56184715/

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