简体   繁体   English

如何获得 numpy 第一个 True Only

[英]how to get numpy where for 1st True Only

i have array of data as given below.我有如下给出的数据数组。

a=np.array([1,2,3,4,5,6,7,8,1,2,3,4,5,6])

i want to get conditional formatting like a>5我想获得像a>5这样的条件格式

i can get it with np.where .我可以用np.where得到它。 But np.where is continuous checking for next true但是 np.where 是连续检查下一个真

np.where(a>5,'T','F')
> array(['F', 'F', 'F', 'F', 'F', 'T', 'T', 'T', 'F', 'F', 'F', 'F', 'F', 'T'], dtype='<U1')

i want to skip the consecutive True Value like when a>5 comes to 6 its True but for 7 also it is True which i dont want.我想跳过连续的真值,比如当a>5到 6 时,它的True值,但对于 7,它也是我不想要的True值。 I want only 1st one.我只想要第一个。

But also if value drops from 7 to 4 and again rise to 6 then that 6 should be True但是,如果值从 7 下降到 4 并再次上升到 6,那么 6 应该是True

You can use np.roll to shift the values and then check if original element is True and shifted element is False您可以使用np.roll移动值,然后检查原始元素是否为 True 并且移动的元素是否为 False

a_bool = a > 5
a_shifted_bool = np.roll(a_bool, 1)
a_shifted_bool[0] = False # first element is last element of a_bool, which we need to ignore

np.where(a_bool & ~a_shifted_bool, 'T', 'F')
array(['F', 'F', 'F', 'F', 'F', 'T', 'F', 'F', 'F', 'F', 'F', 'F', 'F',
       'T'], dtype='<U1')

I guess @Suraj S analyze stock with Best Times of the value to Buy and Sell Stocks.我猜@Suraj S 用最佳时间分析股票来买卖股票。

I try other example, and I think @ggaurav answer is a good way, I learn more in your answer.我尝试了其他示例,我认为@ggaurav 回答是一个好方法,我在您的回答中学到了更多。

import numpy as np

a=np.array([1,2,3,4,5,6,7,8,1,2,3,4,5,6,4,5,6,7,8,4,2,3,6,4,7,9,4,1,2,3,2,5])

a_bool = a > 5
a_shifted_bool = np.roll(a_bool, 1)
a_shifted_bool[0] = False # first element is last element of a_bool, which we need to ignore

np.where(a_bool & ~a_shifted_bool, 'T', 'F')

array(['F', 'F', 'F', 'F', 'F', 'T', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'T', 'F', 'F', 'T', 'F', 'F', 'F', 'F', 'F', 'T', 'F', 'T', 'F', 'F', 'F', 'F', 'F', 'F', 'F'], dtype='<U1')

暂无
暂无

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

相关问题 np.where 检查 True 为第一列然后在另一列 - np.where to check on True for 1st column then on another 从.csv文件创建一个numpy数组,但我只能得到数组中的第一行 - 我错过了什么? - Creating a numpy array from a .csv file but I can only get the 1st line in the array - what am I missing? 如何合并 2 dataframe,创建只出现在第二个 dataframe 但不出现在第一个的行和 groupby 求和? - How to combine 2 dataframe, create a row that appear only in the second dataframe but not in the 1st and groupby to get the sum? webscraping selenium:我的循环总是只得到第一个元素 - webscraping selenium : my loop always get only the 1st element 如何迭代数据框名称列表以获取列表中每个数据框第一行的第一个值 - How to iterate of a list of dataframe's name to get the 1st value of 1st row of every dataframe in the list 如何在两个 arguments function 中仅打印第一个参数? - how to print only 1st argument in two arguments function? 如何在python数据框中的列中获取值的第一次出现 - How to get the 1st occurence of a value in a column in python dataframe 如何在 python 中准确获取字典第一行的元素 - how to get exactly element of 1st row of dictionary in python 如何在 python 中获得一年中的第一个营业日期? - How to get 1st business date of year in python? Python dict更改顺序-如何获得第一次出现? - Python dict changes ordering - how get 1st occurence?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM