如何在累积熊猫系列中找到价值?

时间:2020-04-19 18:45:31

标签: python pandas data-science data-analysis

我有一个熊猫系列,并且已经对其进行过cumsum()。生成的系列看起来像:

A = [10, 25, 30, 20, 27, 29]

我想找到序列中某个值所在的范围。

例如,值28位于(25,30),(30,20)和(27,29)之间。在这种情况下,我想找到最后一个或所有这样的范围。我该如何在熊猫中实现本机并且没有多余的循环?

2 个答案:

答案 0 :(得分:1)

这里是一种可能的numpy方法:

import numpy as np

a = np.array([10, 25, 30, 20, 27, 29])
v = 28 # value to find

# Define intervals
intervals = {i: f'[ {min(i1, i2)}, {max(i1,i2)} ]' for i, (i1,i2) in enumerate(zip(a[:-1],a[1:]))}
intervals
{0: '[ 10, 25 ]', 1: '[ 25, 30 ]', 2: '[ 20, 30 ]', 3: '[ 20, 27 ]', 4: '[ 27, 29]'}

# Find indices of intervals
b = a-v
indices = np.squeeze(np.argwhere(b[:-1] *b[1:]<=0))
indices
array([1, 2, 4], dtype=int64)

[intervals[i] for i in indices]
['[ 25, 30 ]', '[ 20, 30 ]', '[ 27, 29 ]']

答案 1 :(得分:0)

这能回答您的问题吗?

# Define workers and child supervisors to be supervised
children = [
  # Start the Ecto repository
  supervisor(Ssauction.Repo, []),
  # Start the endpoint when the application starts
  supervisor(SsauctionWeb.Endpoint, []),

  supervisor(Absinthe.Subscription, [SsauctionWeb.Endpoint]),
]

children
= if System.get_env("PERIODIC_CHECK") == "ON" do
    Enum.concat(children, [worker(Ssauction.PeriodicCheck, [])])
  else
    children
  end