检测 pandas 中各列之间的值交叉 [英] detecting value crossing between columns in Pandas
问题描述
假设我具有以下数据框:
Let's say I have the following dataframe:
df = pd.DataFrame({'a': [10, 20, 30, 40, 50], 'b': [0, 10, 40, 45, 50]}, columns = ['a', 'b'])
我想列出一个索引列表,其中:
I would like to make a list of indices where:
a [i-1]< b [i]和a [i]> = b [i]
a [i - 1] < b[i] and a[i] >= b[i]
为了检测一个时间序列中的值何时与另一个值交叉
in order to detect when a value, in a timeseries, is crossing another one
是否有一种熊猫惯用的方式来实现这一目标而无需遍历所有元素?
is there a Pandas idiomatic way to achieve this without iterating through all the elements?
我试图通过这样做来创建带有标志的新列,以指示交叉路口:
I tried to create a new column with flags to indicate a crossing by doing this:
df['t'] = (df['a'].shift(1).values < df['b'].values and di['a'].values >= df['b']).astype(bool)
但是不会编译.我不确定如何解决这个问题,只是没有遍历所有元素.
but that won't compile. I'm not sure how to approach this problem, short of doing a loop through all the elements.
推荐答案
您可以使用 Series.lt
,小于" ,与<
和>=相同:
You can use the Series.shift
with Series.lt
which is "less than", same as <
and Series.ge
which is "greater than or equal" and is same as >=
:
mask = df['a'].shift().lt(df['b']) & df['a'].ge(df['b'])
# same as (df['A'].shift() < df['b']) & (df['a'] >= df['b'])
0 False
1 False
2 False
3 False
4 True
dtype: bool
注意,我们不必指定astype(bool)
,pandas可与 boolean indexing
,并在定义条件时返回booleans
.
Notice, we don't have to specify astype(bool)
, pandas works with boolean indexing
and returns booleans
when defining conditions.
要获取带有True
的行的indices
,请使用:
To get the indices
of the rows with True
, use:
idx = df[mask].index.tolist()
print(idx)
[4]
这篇关于检测 pandas 中各列之间的值交叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!