当前位置:网站首页>[Numpy] np.where
[Numpy] np.where
2022-07-29 12:47:00 【山茶花开时。】
1.np.where(condition, x, y)
用法:满足条件condition,输出x,不满足则输出y
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'])
# 添加'总分'字段
df['总分'] = df['语文'] + df['数学'] + df['英语']
# 调用np.where
# 添加'总分评级'字段
df['总分评级'] = np.where(df['总分']>200,'A等','B等')
# np.where也可以用于两个字段之间的比较
df['语文评级'] = np.where(df['语文'] > df['数学'],'√','×')df

2.np.where(condition)
用法:输出满足条件condition(即非0)元素的坐标
import numpy as np
arr = np.array([2,4,6,8,10])
# 返回索引值2,3,4
np.where(arr > 5) # (array([2, 3, 4], dtype=int64),)
# 等价于arr[arr>5]
arr[np.where(arr > 5)] # array([ 6, 8, 10])arr

import numpy as np
arr1 = np.array([[0, 1],[1, 0]])
# 输出arr1中值为1的坐标值
# (array([0,1],dtype=int64), array([1,0],dtype=int64))
np.where(arr1)arr1

边栏推荐
- SIP system composition format
- mysql根据多字段分组——group by带两个或多个参数
- nacos集群搭建
- adb for mysql在什么平台进行开发啊
- nacos cluster construction
- mysql 存储过程详解
- 每日优鲜解散疑云:生鲜电商们苦渡生死劫
- 3D激光SLAM:LeGO-LOAM论文解读---硬件系统部分
- MySql string splitting realizes the split function (field splitting, column switching, row switching)
- ISME | 沈其荣团队韦中组-土壤生物障碍发生的根际微生物组诊断
猜你喜欢
随机推荐
APP local number one-click login
[WeChat applet] WXSS and global, page configuration
学习的时候碰见的一个sql问题,希望大佬们可以解答一二?
What should I do if the webpage is hijacked and redirected?Release net repair method
[WeChat applet] One article to solve button, input, image components
torch使用总结
influxdb2的使用
连接oracle数据库指令
【C语言】扫雷游戏实现(初阶)
How Navicat Connects to MySQL
mysql数据库安装(详细)
[based] GO language. Why do I have to learn Golang and introduction to the language universal
JUC阻塞队列-ArrayBlockingQueue
C# autoCAD 几个经常用到的功能代码。
js进阶四(map、reduce、filter、sort、箭头函数、class继承、yield)
MySQL database installation (detailed)
MySql字符串拆分实现split功能(字段分割转列、转行)
CentOS7安装Oracle数据库的全流程
传奇服务端GOM引擎和GEE引擎区别在哪里?
25年来最经典的电影特效名场面









