python - 熊猫滚动在窗口中获得最频繁的值
问题描述
我想知道如何在时间序列的滚动窗口中获得最频繁的值,即类似
df.rolling(window = 2).rolling.count_values().idxmax()
由于对于数据框的列,可以使用
df["value"].count_values().idxmax()
但是,我只收到一条错误消息阅读 'Rolling' object has no attribute 'count_values'
解决方案
使用rolling().apply
with mode方法:
df.rolling(window = 3).apply(lambda x: x.mode()[0])
x
将是一个pd.Series
长度等于窗口的对象。mode
方法将返回最常见值的列表。lambda
将返回模式列表中的第一项。
推荐阅读
- c# - WebRequest: Content-Type = text/plain;charset=iso-8859-1 : '底层连接已关闭:连接意外关闭。'
- ruby-on-rails - 在 Rails 中 ModelA.transaction do ..end 和 ModelB.transaction do ..end 有什么区别?
- python - 在 Python 中使用 AND 条件查找两个模式之间的字符串
- python - 在 tkinter 中集成绘图会给出更新 idletasks 错误
- sql-server - 我如何改变;A;B;C; 到('A','B','C')?
- docker - mariadb + wordpress 与 docker 的连接错误
- javascript - 在子组件道具上使用父道具
- python - 如何过滤 Python 列表同时保持过滤值为零
- java - 为特定问题添加评论
- javascript - 如何将 HTML与 PHP 函数连接起来?