简体   繁体   English

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

[英]Python Pandas: How to compare values of cells and two columns and maybe using If...Else statement to create another column with new values

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.我正在尝试和研究很多如何做到这一点,但我无法将 pandas 与 if、else 和/或按索引获取值并将其与 if、else 进行比较并分配包含代码/值的列。 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"说明:我有下面这张表,我想比较ID列中的单元格,如果后单元格的值等于前一个单元格,并且如果在COD列中后单元格等于前一个单元格,结果列 =“否”,否则为“通过” ,如果两者都不是,则为“未知”

This is the formula in excel that I made: =IF(B3=B2,IF(C3=C2,"NO","PASS"),"UNKNOWN").这是我在 excel 中做的公式:=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.我什至可以用第一个测试(来自 ID 列单元格)和第二个测试(来自 COD 列单元格)创建两列,并返回 Boolean 结果,但我无法获得 If, Else 将它们连接在一起并在另一列中生成我想要的值。 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()".但结果是:“ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()”。

If you can help me I appreciate it, it can be with pandas or not or mixing.如果你能帮助我,我很感激,它可以与 pandas 或不混合。 I just need it to work.我只需要它工作。 Thank you guys.感谢你们。

Similar approach in pandas will be to use numpy.where function. pandas 中的类似方法将使用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.这似乎更符合您对如何得出结果值的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较一列中的值并使用pandas创建一个新列? - How to compare values in a column and create a new column using pandas? 使用两列中的值在 Pandas 中创建新列 - Create New Column in Pandas Using Values from Two Columns Python Pandas数据框:使用列中的值创建新列 - Python Pandas Dataframe: Using Values in Column to Create New Columns 比较多列中的值并在 Python 中的另一列中添加新值 - Compare values in multiple columns and add a new value in another column in Python Python Pandas - 如何将 dataframe 的两列的值与另一个 Dataframe 的列进行比较? - Python Pandas - How to compare values from two columns of a dataframe to another Dataframe columns? 根据其他两列的值,在 pandas 中创建一个新列 - Create a new column in pandas depending on values from two other columns 如何创建熊猫列并根据另一列中的值填充值 - How to create pandas columns and fill with values according to values in another column 比较python pandas中两列的值/字符串 - Compare values/strings in two columns in python pandas Pandas/Python:如何根据其他列的值创建新列并将额外条件应用于此新列 - Pandas/Python: How to create new column based on values from other columns and apply extra condition to this new column 如何使用python pandas比较来自两个不同csv的单元格值 - How to compare cells values from two different csv using python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM