熊猫:如何获得最先满足条件的行?像变长移位

时间:2020-04-24 05:22:37

标签: python pandas

我有一个用<template v-if="list.isComplete"> ... </template> 索引的表,该表的值removeTask(id)在创建新列removeTodo(list)时要使用。

      <v-scroll-x-transition>
        <template v-if="list.isComplete">
          <v-btn class="ma-2" v-on:click="removeTodo(list)" tile large color="error" icon>
            <v-icon>mdi-trash-can-outline</v-icon>
          </v-btn>
        </template >
      </v-scroll-x-transition>

我想生成一列schema,该列在前一天的最后一个价格行中返回select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'Table1' or COLUMN_Name = 'col1'; 的值,因此输出如下:

date

到目前为止,我想出的唯一方法是使用price,它逐行迭代,并且对于每一行都会过滤最近一天的最后一行的索引。但是,即使DataFrame是按日期索引的,也要花费很多时间。对于具有十万行的表,填充需要几分钟。

我想知道是否有任何方法可以向量化形式创建新系列;类似于df.shift(num_periods),但num_periods根据行的previous_close值进行了调整。

1 个答案:

答案 0 :(得分:1)

我建议像question中那样为重新编制索引部分:


import pandas as pd
import numpy as np
np.random.seed(123)
df = pd.DataFrame({"date": pd.date_range("2019-01-01 22:00:00", periods=10, freq="H"),
                   "price": np.random.randint(1, 100, 10)})
df = df.set_index("date")

df = pd.concat([df.price, 
           df.resample("d").last().shift().rename(columns={"price":"close"}).reindex(df.index, method='ffill')], 
           axis = 1)

然后您得到结果:

                    price  close
date                             
2019-01-01 22:00:00     67    NaN
2019-01-01 23:00:00     93    NaN
2019-01-02 00:00:00     99   93.0
2019-01-02 01:00:00     18   93.0
2019-01-02 02:00:00     84   93.0
2019-01-02 03:00:00     58   93.0
2019-01-02 04:00:00     87   93.0
2019-01-02 05:00:00     98   93.0
2019-01-02 06:00:00     97   93.0
2019-01-02 07:00:00     48   93.0

编辑: 如果您的工作日结束于2,并且您希望在这一小时内关闭,建议您在日期上使用DateOffset(与here相同),并执行相同的方法:

df = pd.DataFrame({"date": pd.date_range("2019-01-01 22:00:00", periods=10, freq="H"),
                   "price": np.random.randint(1, 100, 10)})
df["proxy"] = df.date + pd.DateOffset(hours=-3)
df = df.set_index("proxy")
df = pd.concat([df[["price", "date"]], 
          (df.price.resample("d").last().shift()
                   .rename({"price":"close"})
                   .reindex(df.index, method='ffill'))],
          axis = 1).reset_index(drop=True).set_index("date")

您得到结果:

                     price  price
date                             
2019-01-01 22:00:00     67    NaN
2019-01-01 23:00:00     93    NaN
2019-01-02 00:00:00     99    NaN
2019-01-02 01:00:00     18    NaN
2019-01-02 02:00:00     84    NaN
2019-01-02 03:00:00     58   84.0
2019-01-02 04:00:00     87   84.0
2019-01-02 05:00:00     98   84.0
2019-01-02 06:00:00     97   84.0
2019-01-02 07:00:00     48   84.0