[Numpy] np.select

 美图欣赏2022/07/28

np.select(condlist, choicelist, default=0) 

condlist:条件列表,元素是布尔型数组

choicelist:列表,数组元素

default:当所有条件都不满足时,用default值(默认值)填上

注意: condlist, choicelist的长度必须一致,每一个条件都要进行一次元素选择或者操作 

import pandas as pd
import numpy as np
 
df = pd.DataFrame(data={'语文':[50,90,70,78,60],
                        '数学':[59,80,60,75,69],
                        '英语':[61,95,65,80,59]},
                  index=['Harry','Andy','Rita','Lee','Jack'])
# 多条件判断,有多个条件可以使用np.select
conditions = [(df['语文']>=60)&(df['数学']>=60)&(df['英语']>=60),
              (df['语文']<60)&(df['数学']>=60)&(df['英语']>=60),
              (df['语文']>=60)&(df['数学']<60)&(df['英语']>=60),
              (df['语文']>=60)&(df['数学']>=60)&(df['英语']<60),
              (df['语文']>=60)&(df['数学']<60)&(df['英语']<60),
              (df['语文']<60)&(df['数学']>=60)&(df['英语']<60),
              (df['语文']<60)&(df['数学']<60)&(df['英语']>=60),
              (df['语文']<60)&(df['数学']<60)&(df['英语']<60)]
results = ['合格','不合格','不合格','不合格','不合格','不合格','不合格','不合格']
# 生成'评级'字段
df['评级'] = np.select(conditions,results,default='不合格')

df

As an AI language model, I cannot provide a definitive answer without additional context and information about the variables and the code block. However, I can provide some general information about the np.where() function and the code snippet you provided. The np.where() function is a NumPy function that returns an array of elements from x or y, depending on the condition. The syntax of np.where() is as follows: np.where(condition, x, y) - condition: A boolean array or a condition that evaluates to a boolean array. - x: An array-like object that has the same shape as the condition. It is used to select elements where the condition is True. - y: An array-like object that has the same shape as the condition. It is used to select elements where the condition is False. In the code snippet you provided, the np.where() function is used to return an array of values where the condition is True, or np.inf (positive infinity) where the condition is False. The condition is: Self_Time < Hour[np.min(np.where(Hour == Country_Time[i])) + 1] This condition compares the values in the Self_Time array with the value in the Hour array at the index returned by np.min(np.where(Hour == Country_Time[i])) + 1. The np.min() function returns the index of the minimum value in the array returned by np.where(). The +1 is added to the index to get the next value in the Hour array. Overall, the code block you provided seems to be part of a larger program that involves time calculations and comparisons. Without additional context, it is difficult to provide a more specific explanation.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值