Python Pandas:如何比较单元格和两列的值,并可能使用 If...Else 语句创建具有新值的另一列

发布于 2025-01-16 22:37:09 字数 1359 浏览 3 评论 0原文

我正在尝试和研究如何做到这一点,但是我在将 pandas 与 if、else 和/或按索引获取值并将其与 if、else 进行比较并分配带有代码/值的列时遇到了麻烦。 说明:我有下面这张表,我想比较 ID 列中的单元格,如果后一个单元格的值等于前一个单元格AND if in COD 列后部细胞等于前一个细胞,THEN 结果列 =“否”,否则“通过”,如果两者都不是,则“未知”

这是Excel中的公式我做的:= IF(B3 = B2,IF(C3 = C2,“否”,“通过”),“未知”)。

输入图片这里的描述

下面我也发布了一些代码尝试。我什至可以使用第一个测试(来自 ID 列单元格)和第二个测试(来自 COD 列单元格)创建两列,并返回布尔结果,但我无法使用 If, Else 将它们连接在一起并在另一列中生成我想要的值。 如果有人可以帮助我,我会很感激吗?

df = df.sort_values(by=['ID'])
df['matchesID'] = df['ID'].shift(1) == df['ID']
df['matchesCod']= df['Cod'].shift(1) == df['Cod']

或者

df = df.sort_values(by=['ID'])
test = (df['SWENo'].shift(1) == df['SWENo']) & (df['Cod'].shift(1) == df['Cod'])

我正在尝试类似下面的内容

if df['ID'].shift(1) == df['ID'] and df['Cod'].shift(1) == df['Cod']:
    listProg.append('not')
elif df['ID'].shift(1) == df['ID'] and df['Cod'].shift(1) != df['Cod']:
    listProg.append('pass')
else:
    listProg.append('Unknown')

但结果是:“ValueError:系列的真值不明确。使用a.empty,a.bool(),a.item(),a.any()或a。全部()”。

如果你能帮助我,我很感激,它可以与熊猫一起,也可以不与熊猫一起,也可以与熊猫混合。我只需要它工作。谢谢你们。

I'm trying and researching a lot how to do this, but I'm having trouble mixing pandas with if, else and/or get values by index and compare it with if, else and assign a column with codes/values.
Explanation: I have this table below, I want to compare the cells in the ID column, and if the value of the posterior cell is equal to the previous one AND if in the COD column the posterior cell is equal to the previous one, THEN Result column = "no", otherwise "pass" and if neither then "unknown"

This is the formula in excel that I made: =IF(B3=B2,IF(C3=C2,"NO","PASS"),"UNKNOWN").

enter image description here

Below I have also posted some code attempts. I can even create two columns with the first test (from the ID column cells) and the second test (from the COD column cells), and return with Boolean results, but I can't get the If, Else to join it all together and generate the values I want in another column.
Would I appreciate it if someone could help me?

df = df.sort_values(by=['ID'])
df['matchesID'] = df['ID'].shift(1) == df['ID']
df['matchesCod']= df['Cod'].shift(1) == df['Cod']

or

df = df.sort_values(by=['ID'])
test = (df['SWENo'].shift(1) == df['SWENo']) & (df['Cod'].shift(1) == df['Cod'])

I was trying something like this below

if df['ID'].shift(1) == df['ID'] and df['Cod'].shift(1) == df['Cod']:
    listProg.append('not')
elif df['ID'].shift(1) == df['ID'] and df['Cod'].shift(1) != df['Cod']:
    listProg.append('pass')
else:
    listProg.append('Unknown')

But the result is: "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()".

If you can help me I appreciate it, it can be with pandas or not or mixing. I just need it to work. Thank you guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2025-01-23 22:37:09

pandas 中的类似方法是使用 numpy.where 函数。

使用此代码:

import numpy as np

df['Result'] = np.where(df['ID'] == df['ID'].shift(), np.where(df['Cod'] == df['Cod'].shift(), 'NO', 'PASS'), 'UNKNOWN')

我得到以下结果:

   ID  Cod   Result
0   1    1  UNKNOWN
1   2    1  UNKNOWN
2   2    1       NO
3   3    1  UNKNOWN
4   4    1  UNKNOWN
5   4    2     PASS
6   4    2       NO
7   5    1  UNKNOWN
8   6    1  UNKNOWN

这似乎更符合您对结果值如何导出的描述。

Similar approach in pandas will be to use numpy.where function.

With this code:

import numpy as np

df['Result'] = np.where(df['ID'] == df['ID'].shift(), np.where(df['Cod'] == df['Cod'].shift(), 'NO', 'PASS'), 'UNKNOWN')

I get below results:

   ID  Cod   Result
0   1    1  UNKNOWN
1   2    1  UNKNOWN
2   2    1       NO
3   3    1  UNKNOWN
4   4    1  UNKNOWN
5   4    2     PASS
6   4    2       NO
7   5    1  UNKNOWN
8   6    1  UNKNOWN

which seems more inline with your description of how Result value is derived.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文