- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用来自 here 的相同示例但只需将“A”列更改为可以轻松分组的内容:
import pandas as pd
import numpy as np
# Get some time series data
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/timeseries.csv")
df["A"] = pd.Series([1]*3+ [2]*8)
df.head()
现在的输出是:
Date A B C D E F G
0 2008-03-18 1 164.93 114.73 26.27 19.21 28.87 63.44
1 2008-03-19 1 164.89 114.75 26.22 19.07 27.76 59.98
2 2008-03-20 1 164.63 115.04 25.78 19.01 27.04 59.61
3 2008-03-25 2 163.92 114.85 27.41 19.61 27.84 59.41
4 2008-03-26 2 163.45 114.84 26.86 19.53 28.02 60.09
5 2008-03-27 2 163.46 115.40 27.09 19.72 28.25 59.62
6 2008-03-28 2 163.22 115.56 27.13 19.63 28.24 58.65
当我们假设它是一个列表时,计算累积总和(来自链接问题的代码)效果很好:
# Put your inputs into a single list
input_cols = ["B", "C"]
df['single_input_vector'] = df[input_cols].apply(tuple, axis=1).apply(list)
# Double-encapsulate list so that you can sum it in the next step and keep time steps as separate elements
df['single_input_vector'] = df.single_input_vector.apply(lambda x: [list(x)])
# Use .cumsum() to include previous row vectors in the current row list of vectors
df['cumulative_input_vectors1'] = df["single_input_vector"].cumsum()
但在这种情况下,我如何cumsum
按“A”分组的列表?我希望这会起作用,但它不起作用:
df['cumu'] = df.groupby("A")["single_input_vector"].apply(lambda x: list(x)).cumsum()
代替 [[164.93, 114.73, 26.27], [164.89, 114.75, 26....
我填入了一些行,其他行是 NaN。这就是我想要的(cols [B,C] 累积到 col A 组中):
A cumu
0 1 [[164.93,114.73], [164.89,114.75], [164.63,115.04]]
0 2 [[163.92,114.85], [163.45,114.84], [163.46,115.40], [163.22, 115.56]]
此外,我该如何高效地执行此操作?我的数据集很大(大约 200 万行)。
最佳答案
它看起来不像你在做算术和,更像是沿着 axis=1 的 concat
首先groupby和concat
temp_series = df.groupby('A').apply(lambda x: [[a,b] for a, b in zip(x['B'], x['C'])] )
0 [[164.93, 114.73], [164.89, 114.75], [164.63, ...
1 [[163.92, 114.85], [163.45, 114.84], [163.46, ...
然后转换回数据框
df = temp_series.reset_index().rename(columns={0: 'cumsum'})
一行
df = df.groupby('A').apply(lambda x: [[a,b] for a, b in zip(x['B'], x['C'])] ).reset_index().rename(columns={0: 'cumsum'})
关于python - Pandas group by cumsum of lists - 为 lstm 做准备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59013978/
我正在尝试在 pandas 中找到一个矢量化解决方案,该解决方案在电子表格中很常见,即在基于实际 cumsum 的结果跳过或设置固定值的情况下进行 cumsum。我有以下内容: A 1 0
假设我有一个距离数组 x=[1,2,1,3,3,2,1,5,1,1]。 我想从 x 中获取索引,其中 cumsum 达到 10,在本例中,idx=[4,9]。 所以 cumsum 在满足条件后重新启动
每当超过给定的 cumsum 阈值时,我需要有关对连续行进行分组的帮助。当超过此阈值时,也应重新启动 cumsum(为零),如下所示: Index Values Regular CumS
我对 python 还很陌生。我尝试对每个客户进行累积总和,以查看相应的不活动月份(标志:1 或 0)。因此,当我们有 0 时,需要重置 1 的累积和。当我们有新客户端时,也需要重置。请参阅下面的示例
我要重置 cumsum在一个向量上,当它达到某个值时。 例如。对于以下向量: v <- c(3, 5, 2, 5, 3, 4, 5, 3, 1, 4) 预期输出为: c(0, 0, 10, 0, 0,
我正在尝试使用reduce函数在python中编写累积和的版本。这是到目前为止我的代码: from functools import reduce def my_cum_sum(arg): r
问题:给定一个二叉搜索树,其中的键是数字,我们将定义操作“cumsum”(累积和的简写),它切换树中每个节点的键所有小于或等于它的键的总和。 例如, 在这个例子中, 根中的键 5 切换为值 10:根中
我是 pandas 的新手,我可以将 cumsum 添加为 df.cumsum(axis=1) y0 y1 y2 0 2 3 4 1 2 2 3 2 0 0
我有一个正数向量,使得 - 如果数字大于或等于 1,则相加 - 如果数字小于 1,则乘以累积和。 例如 > set.seed(0) > x x [1] 3.0 0.9 0.9 1.0 3.0 0.
我想计算cumsum从每次运行信号开始的某个值,其中 signal == 1 . 示例数据: set.seed(123) df head(df,12) Date value sig
这个问题在这里已经有了答案: Cumsum with reset when 0 is encountered and by groups (2 个回答) Cumulative sum that res
dict={"asset":["S3","S2","E4","E1","A6","A8"], "Rank":[1,2,3,4,5,6],"number_of_attributes":[2,1,2,2,
这个问题在这里已经有了答案: Restart cumsum and get index if cumsum more than value (3 个答案) 关闭 2 年前。 我想在每次超过阈值 10
我想修改 cumsum 函数。我想将负值更改为 0。并且当有一个不同于 0 的新 D 值时,则使用 D-S。 下面的例子: TD % group_by(product) %>% mutate(res
是否可以在一列中迭代使用 cumsum() 并在另一列上以开始 - 停止为条件: 给定数据帧 df 和一列 X 其中值是递增的。 cumsum() 应在达到 10 或 10 的倍数时停止(例如 20、
带有数据框: df <- data.frame(id = rep(1:3, each = 5) , hour = rep(1:5, 3)
In [46]: d = np.random.randn(10, 1) * 2 In [47]: df = pd.DataFrame(d.astype(int), columns=['data'])
我目前正在尝试创建一个累积总和列,该列将根据 Game_ID 创建一个累积总和,但一次只计算与 Game_ID 相关的值。例如,玩家 A 在 Game_ID == 1 中拍摄 20 次,在 Game_
我正在尝试计算每组从最后一行到第一行的cumsum。 示例数据: t1 ts var val val2 1 a 0 2 2 a 0 2 3 a 0
我有一个矩阵,例如: A= [ 1 2 3 4 3 5 6 6 4 1 2 3 ] 我想以另一个矩阵(或数据框)的形式获取列的累积和。例如,这个矩阵会给出: B= [1 2 3 4
我是一名优秀的程序员,十分优秀!