小编Enr*_*ini的帖子

获取Pandas滚动窗口的索引

我想获取 Pandas Series 的每个滚动窗口中元素的索引。

对我有用的解决方案来自对现有问题的回答:我答案中描述的函数中获得了window.index每个值。我只对上述功能感兴趣。windowrollingstep=1

但这个函数并不是特定于 DataFrames 和 Series 的,它可以在基本的 Python 列表上工作。是否有一些功能可以利用 Pandas 的滚动操作?

我尝试了Rolling.apply方法:

s = pd.Series([1, 2, 3, 4, 5, 6, 7])

rolling = s.rolling(window=3)
indexes = rolling.apply(lambda x: x.index)
Run Code Online (Sandbox Code Playgroud)

但它的结果是TypeError: must be real number, not RangeIndex. 显然,该Rolling.apply方法仅接受基于每个窗口返回数字的函数。这些函数不能返回其他类型的对象。

Rolling我可以使用Pandas 类的其他方法吗?甚至是私有方法。

或者还有其他 Pandas 特定的功能来获取重叠滚动窗口的索引吗?

预期产出

作为输出,我期望某种列表对象。每个内部列表应该计算每个窗口的索引值。原始s系列有[0, 1, 2, 3, 4, 5, 6]索引。因此,使用 a 滚动window=3,我期望结果如下:

[
    [0, 1, 2],
    [1, …
Run Code Online (Sandbox Code Playgroud)

python pandas rolling-computation

5
推荐指数
1
解决办法
2091
查看次数

SQLAlchemy 根据另一个表中的值过滤表的最佳方法

如果我的问题很平庸,我提前道歉:我是 SQL 的初学者。

我想创建一个简单的数据库,有两个表:StudentsAnswers。基本上,每个学生将回答三个问题(每个问题可能的答案是TrueFalse),他的答案将存储在Answers表中。 Students可以有两个“经验”级别:“本科生”和“研究生”。获得“研究生”经验级别Answers所提供的所有内容的最佳方式是什么?Students

这就是我为StudentsAnswers表中的条目定义 SQLAlchemy 类的方式:

import random

from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, Date, Boolean, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship


db_uri = "sqlite:///simple_answers.db"
db_engine = create_engine(db_uri)
db_connect = db_engine.connect()

Session = sessionmaker()
Session.configure(bind=db_engine)
db_session = Session()


Base = declarative_base()    


class Student(Base):
    __tablename__ = "Students"
    
    id = Column(Integer, primary_key=True)
    experience = …
Run Code Online (Sandbox Code Playgroud)

python sql sqlalchemy

1
推荐指数
1
解决办法
8302
查看次数

标签 统计

python ×2

pandas ×1

rolling-computation ×1

sql ×1

sqlalchemy ×1