[英]IF + AND Statements
I have a 'Current Result' in the form of a data frame in Python (depicting in Excel as an illustration).我在 Python 中有一个数据框形式的“当前结果”(以 Excel 为例)。
I'd like to add a column that classifies whether a row is a 'PRIME' or an 'ALT' designation.我想添加一个列来分类一行是“PRIME”还是“ALT”名称。
The rules for whether something is a 'ALT' is both of the following:某物是否为“ALT”的规则如下:
Is a nested IF / AND statement the only concept that will work or is there a more efficient way to do this?嵌套的 IF / AND 语句是唯一可行的概念,还是有更有效的方法来做到这一点? PS I am newer to Python. PS我对Python比较陌生。
ADDING EXAMPLE CODE OF A DATA FRAME:添加数据帧的示例代码:
import pandas as pd
### Generate data from lists.
data = {'Part':['AAA', 'BBB', 'CCC', 'DDD','EEE','FFF','GGG', 'HHH', 'III', 'JJJ', 'LLL','MMM','NNN'], 'Group':['', '', '', '','','45','45', '45', '', 'FF', 'FF','7J','7J'], 'Utl':['0', '0', '0', '0','0','100','0', '0', '0', '100', '0','100','0']}
### Create DataFrame
df = pd.DataFrame(data)
### Print the output.
print(df)
Its kind of hard to re-create without digestible data, but I believe this will get you what you need IIUC如果没有可消化的数据,它很难重新创建,但我相信这会让你得到你所需要的 IIUC
data = {
'Group' : [np.nan, None, np.nan, '7f', '7f', '7f', None, None, None, '4j', '4j', None, None, '4j'],
'Utilization':[0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 100, 0, 0, 99]
}
df = pd.DataFrame(data)
#This is here just in case you have some np.nan's instead of None it'll make everything the same
df['Group'] = df['Group'].replace({np.nan : None})
condition_list = [
(df['Group'].values == None) | ((df['Utilization']%2 == 0) & (df['Utilization'] > 0)),
(df['Group'].values != None) & (df['Utilization']%2 != 0) & (df['Utilization'] > 0),
(df['Group'].values != None) & (df['Utilization'] == 0)
]
choice_list = ['Prime', 'Alt', 'Alt']
df['Check'] = np.select(condition_list, choice_list, 0)
df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.