Python Pandas:如何比较单元格和两列的值,并可能使用 If...Else 语句创建具有新值的另一列
我正在尝试和研究如何做到这一点,但是我在将 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").
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pandas 中的类似方法是使用 numpy.where 函数。
使用此代码:
我得到以下结果:
这似乎更符合您对结果值如何导出的描述。
Similar approach in pandas will be to use
numpy.where
function.With this code:
I get below results:
which seems more inline with your description of how Result value is derived.