在DataFrame中进行迭代并写下满足条件的值的索引 [英] Iterating in DataFrame and writing down the index of the values where a condition is met
问题描述
我有一个由20行和2500列组成的数据. 每列都是唯一的乘积,行是时间序列,测量结果.因此,每种产品都要进行20次测量,总共有2500种产品.
I have a data made of 20 rows and 2500 columns. Each column is a unique product and rows are time series, results of measurements. Therefore each product is measured 20 times and there are 2500 products.
我的数据定义为DataFrame
,我想写下所有列(产品,这样我最后会有一个数组.
My data is defined as DataFrame
and I want to write down the number of the row (index) where a specific condition (such as: x> 3
) is met for the first time, for all columns(products, so that I will have an array in the end.
我尝试使用循环和迭代,但是执行失败.
I tried using loops and iterrow but failed at executing.
P.S:我使用idxmax()
来获取最大值的行ID,但这一次我想获取第一次满足条件然后中断的单元格的索引.
P.S: I used idxmax()
in order to get the row id of max value but this time I want to get the index of the cell where a condition is met for the first time and then break.
推荐答案
只需使用.gt
+ .idxmax
,这将为您第一次满足条件提供索引.
Simply use .gt
+ .idxmax
, which will give you the index of the first time your condition is met.
import pandas as pd
import numpy as np
np.random.seed(12)
df = pd.DataFrame(np.random.randint(1,5,(20,2500)))
df.gt(3).idxmax()
#0 0
#1 0
#2 4
#3 4
#4 1
#...
#2496 8
#2497 0
#2498 5
#2499 1
这篇关于在DataFrame中进行迭代并写下满足条件的值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!