熊猫:在时间序列中查找第一个条件

时间:2020-11-03 02:28:47

标签: python pandas numpy

我想获得时间序列数据集中小于-3的值的第一次出现。我在使用熊猫和避免显式迭代/循环时遇到困难。假设我有下表:

Date          Return
2020-10-12   -1.501909
2020-10-13   -2.123128
2020-10-14   -3.771381
2020-10-15   -4.919922
2020-10-16   -3.006824
2020-10-19   -2.492342
2020-10-20   -1.040846
2020-10-21   -2.251543
2020-10-22   -3.751839
2020-10-23   -3.420191
2020-10-26   -5.215563
2020-10-27   -2.502344
2020-10-28   -2.836966
2020-10-29   -3.747811
2020-10-30   -3.866789

我正在寻找的结果是仅获得小于-3的第一个值。所以结果看起来像这样

Date         Return
2020-10-14   -3.771381
2020-10-22   -3.751839
2020-10-29   -3.747811

理想情况下,在新列中获得小于-3的计数值也很好

Date         Return     streak
2020-10-14   -3.771381  3
2020-10-22   -3.751839  3
2020-10-29   -3.747811  2

任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:0)

是的,使用过滤器:

df = df[df['Return'] < -3]

答案 1 :(得分:0)

new Vue({ components: { Multiselect: window.VueMultiselect.default }, data: { users: [], usersArray: [{ id: 1, name: "A" }, { id: 2, name: "B" }, { id: 3, name: "C" }, { id: 4, name: "D" }, ] }, }).$mount('#app')将这些值按一个位置排列,因此可以在“一次性”索引处进行比较。 从那里开始,只需对偶条件进行过滤

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-multiselect@2.1.6"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-multiselect@2.1.0/dist/vue-multiselect.min.css">
<div id="app">
  <multiselect v-model="users" :options="usersArray.map(user => user.id)" :custom-label="opt => usersArray.find(x => x.id == opt).name" :multiple="true" :clear-on-select="false" :preserve-search="false">
  </multiselect>
  <pre>{{ users }}</pre>
</div>

有了这些线索,您可以处理编码细节吗?