- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Python 和数据科学还不太熟悉。
我有这两个数据框:df 数据框
df = pd.DataFrame({"Date": ['2014-11-21 11:00:00', '2014-11-21 11:00:03', '2014-11-21 11:00:04', '2014-11-21 11:00:05', '2014-11-21 11:00:07', '2014-11-21 11:00:08', '2014-11-21 11:00:10', '2014-11-21 11:00:11', '2014-10-24 10:00:55', '2014-10-24 10:00:59'], "A":[1, 2, 5, 3, 9, 6, 3, 0, 8, 10]})
Date A
0 2014-11-21 11:00:00 1
1 2014-11-21 11:00:03 2
2 2014-11-21 11:00:04 5
3 2014-11-21 11:00:05 3
4 2014-11-21 11:00:07 9
5 2014-11-21 11:00:08 6
6 2014-11-21 11:00:10 3
7 2014-11-21 11:00:11 0
8 2014-10-24 10:00:55 8
9 2014-10-24 10:00:59 10
info Dataframe,此数据框包含我的最终 df 应包含的日期时间范围
info = pd.DataFrame({"Start": ['2014-11-21 11:00:00', '2014-11-21 11:08:00', '2014-10-24 10:55:00'], "Stop": ['2014-11-21 11:07:00', '2014-11-21 11:11:00', '2014-10-24 10:59:00']})
Start Stop
0 2014-11-21 11:00:00 2014-11-21 11:00:07
1 2014-11-21 11:00:08 2014-11-21 11:00:11
2 2014-10-24 10:00:55 2014-10-24 10:00:59
目标是使用两秒窗口
计算df
中的累积和,当且仅当df
中的实际行是在 info
中的某一行的范围内。例如,日期为 2014-11-21 11:00:08
的行的累积总和应为 0
。因为它位于范围的开头,另一个示例是日期为 2014-11-21 11:00:07
的行,其总和应为 12(9+3)
.
这是我到目前为止所取得的成就:
import pandas as pd
import numpy as np
df = pd.DataFrame({"Date": ['2014-11-21 11:00:00', '2014-11-21 11:00:03', '2014-11-21 11:00:04', '2014-11-21 11:00:05', '2014-11-21 11:00:07', '2014-11-21 11:00:08', '2014-11-21 11:00:10', '2014-11-21 11:00:11', '2014-10-24 10:00:55', '2014-10-24 10:00:59'], "A":[1, 2, 5, 3, 9, 6, 3, 0, 8, 10]})
info = pd.DataFrame({"Start": ['2014-11-21 11:00:00', '2014-11-21 11:00:08', '2014-10-24 10:00:55'], "Stop": ['2014-11-21 11:00:07', '2014-11-21 11:00:11', '2014-10-24 10:00:59']})
#info = pd.DataFrame({"Start": ['2014-11-21 11:00:00', '2014-11-21 11:00:00', '2014-11-21 11:00:00', '2014-11-21 11:00:01', '2014-11-21 11:00:02', '2014-11-21 11:00:03', '2014-11-21 11:00:04', '2014-11-21 11:00:05'], "Stop": ['2014-11-21 11:00:00', '2014-11-21 11:00:01', '2014-11-21 11:00:02', '2014-11-21 11:00:03', '2014-11-21 11:00:04', '2014-11-21 11:00:05', '2014-11-21 11:00:06', '2014-11-21 11:00:07']})
info['groupnum']=info.index
info.Start=pd.to_datetime(info.Start)
info.Stop=pd.to_datetime(info.Stop)
cinfo = info.set_index(pd.IntervalIndex.from_arrays(info.Start, info.Stop, closed='both'))['groupnum']
df['groupnum']=pd.to_datetime(df.Date).map(cinfo)
df['cum'] = df.groupby('groupnum').A.cumsum()
print(df)
预期结果:
Date A groupnum cum
0 2014-11-21 11:00:00 1 0 1
1 2014-11-21 11:00:03 2 0 2
2 2014-11-21 11:00:04 5 0 7
3 2014-11-21 11:00:05 3 0 10
4 2014-11-21 11:00:07 9 0 12
5 2014-11-21 11:00:08 6 1 6
6 2014-11-21 11:00:10 3 1 9
7 2014-11-21 11:00:11 0 1 3
8 2014-10-24 10:00:55 8 2 8
9 2014-10-24 10:00:59 10 2 10
实际结果:
Date A groupnum cum
0 2014-11-21 11:00:00 1 0 1
1 2014-11-21 11:00:03 2 0 3
2 2014-11-21 11:00:04 5 0 8
3 2014-11-21 11:00:05 3 0 11
4 2014-11-21 11:00:07 9 0 20
5 2014-11-21 11:00:08 6 1 6
6 2014-11-21 11:00:10 3 1 9
7 2014-11-21 11:00:11 0 1 9
8 2014-10-24 10:00:55 8 2 8
9 2014-10-24 10:00:59 10 2 18
但是这是对 groupnum 进行累积和,我无法仅累积 2 秒。
那么有什么适当的方法来实现这一目标吗?我将不胜感激。
我的英语不太好,希望我能正确解释你的意思
最佳答案
此方法可能不适用于 100M 行数据框
要创建 groupnum 列,您可以 ufunc.outer与 greater_equal
和 less_equal
将 df
中的每个时间与 info
中的每个开始和停止进行比较,并使用 argmax
逐行获取其 True 位置。然后,您可以在此列上groupby
,并在 2 秒上滚动
# create an boolean array to find in which range each row is
arr_bool = ( np.greater_equal.outer(df.Date.to_numpy(), info.Start.to_numpy())
& np.less_equal.outer(df.Date.to_numpy(), info.Stop.to_numpy()))
# use argmax to find the position of the first True row-wise
df['groupnum'] = arr_bool.argmax(axis=1)
# select only rows within ranges, use set_index for later rolling and index alignment
df = df.loc[arr_bool.any(axis=1), :].set_index('Date')
# groupby groupnum, do the sum for a closed interval of 2s
df['cum'] = df.groupby('groupnum').rolling('2s', closed = 'both').A.sum()\
.reset_index(level=0, drop=True) # for index alignment
df = df.reset_index() # get back date as a column
print (df)
Date A groupnum cum
0 2014-11-21 11:00:00 1 0 1.0
1 2014-11-21 11:00:03 2 0 2.0
2 2014-11-21 11:00:04 5 0 7.0
3 2014-11-21 11:00:05 3 0 10.0
4 2014-11-21 11:00:07 9 0 12.0
5 2014-11-21 11:00:08 6 1 6.0
6 2014-11-21 11:00:10 3 1 9.0
7 2014-11-21 11:00:11 0 1 3.0
8 2014-10-24 10:00:55 8 2 8.0
9 2014-10-24 10:00:59 10 2 10.0
编辑:如果arr_bool无法以这种方式创建您可以尝试迭代 info
的行并独立检查它是否高于 start 且低于 stop:
# get once an array of all dates (should be faster)
arr_date = df.Date.to_numpy()
# create groups by sum
df['groupnum'] = np.sum([i* (np.greater_equal(arr_date, start)&np.less_equal(arr_date, stop))
for i, (start, stop) in enumerate(zip(info.Start.to_numpy(), info.Stop.to_numpy()), 1)], axis=0) - 1
# remove the rows that are not in any range
df = df.loc[df['groupnum'].ge(0), :].set_index('Date')
# then same for the column cum
df['cum] = ...
关于python - 使用范围数据集返回 2 秒的累积和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59734481/
美好的一天! 我正在制作一个出勤检查程序,单击一次时显示橙色按钮,单击两次时显示红色按钮,单击 3 次时显示黑色按钮。我在如何累积 getClickCount() 值方面遇到问题,因为对于按钮要注册
我似乎无法在 Adobe 网站上找到明确的答案。使用 ColdFusion 10,11 甚至 2016,更新(修补程序)是否累积? 例如,ColdFusion 的修补程序高达 hotfix_023
我是随机森林新手,我有一个关于回归的问题。我正在使用 R 包 randomForests 来计算 RF 模型。 我的最终目标是选择对预测连续性状很重要的变量集,因此我正在计算一个模型,然后删除准确度平
目前我们有一个发布/消费者服务,消费者将收到的消息写入 AWS S3。我们目前每月编写超过 100.000.000 个对象。但是,我们可以根据一些规则对这些消息进行分组,以节省一些钱。这些规则可以是这
假设我有一个二叉树: data BinTree a = Nil | Branch a (BinTree a) (BinTree a) 我想在这样的结构上做一个累积映射: mapAccum ::
我正在使用内核估计,我应用了 density函数从 R 到我的数据文件(双变量),经过几次统计处理后,我需要转换这些数据,这就是我的问题: 是否有非参数方法的逆累积分布函数? 我尝试过 Google、
不确定以前是否有人问过这个问题,尝试搜索它但找不到任何相关内容。 我试图获得一个累积的字符串聚合,即仅运行不同值的聚合。这是我正在寻找的结果的示例。 我尝试使用 string_agg 函数,但它仅在用
我想找到累积的 bind.rows。这是我想要实现的小例子。我将使用 dslabs 包中的 gapminder 数据集进行演示。 library(tidyverse) library(dslabs)
在 Linux 中使用 tomcat 进程时,我们观察到时间字段显示5506:34(累积 CPU 时间)。在探索时,这是在进程的整个生命周期中运行所花费的 CPU 时间的百分比。 由于这是一个 Jav
我有一些数据可以使用 pyparsing 中的 OneorMore 函数进行解析。比如, fun = OneorMore( foo.setResultsName("foo") + bar.setRe
我试图弄清楚是否有一种简单的方法可以解决 pandas 的以下问题。假设我有四个容器,A、B、C、D,每个容器都有特定的体积。假设我现在得到了一定量的液体,我想用它来填充这些容器。我怎样才能想出一个“
我正在尝试编写一个函数来检测所有上升沿 - 向量中值超过特定阈值的索引。这里描述了类似的东西:Python rising/falling edge oscilloscope-like trigger
这个问题在这里已经有了答案: Multiplying elements of a column in skipping an element after each iteration (3 个答案)
有没有办法获取数据框中每一行的值计数? |f1|f2| ------- v1 | a value_counts -> {a:1} v2 | a value_counts -> {a:2} v3 |
我目前正在尝试对我正在构建的计算器(使用复合模式)进行测试。第一种方法应该添加 75 美元,效果很好,但是当第二种方法运行时,“服务”被重置并且有0 美元作为工作成本。如果我将这两种方法合二为一,那么
我有一个如下所示的文档: 数据.txt 100, "some text" 101, "more text" 102, "even more text" 我使用正则表达式处理它并返回一个新的处理文档,如
假设我有这个: function getAllPromises(key: string, val: any): Promise { const subDeps = someHash[key]; c
我在 mysql 中有表“cumul_sum”,我想根据条件划分“cumulative”列,即如果此列中的值 >= 70,则这些值应存储在名为“others”的新列中"并且前面应该存放对应的sku_i
我正在做一个用 C++ 刺激 ATM 的项目,但在使用累加器时遇到了一些问题,我的问题是:我正在使用开关(这里是情况 1)来更改在包含的函数中声明的 2 个变量的值switch(),但是值只在情况 1
我希望能够使用 accumulate 对 vector 中的每隔一对元素进行累加。我尝试了以下但没有成功,为非空、非零 vector 返回错误 return std::accumulate(vec.b
我是一名优秀的程序员,十分优秀!