pandas按列表累加分组-为LSTM做准备

3

使用与此处相同的示例,但只需将“ 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()

但是在这种情况下,如何按 “A” 分组对列表进行 cumsum?我原以为这会起作用,但它却没有:

df['cumu'] = df.groupby("A")["single_input_vector"].apply(lambda x: list(x)).cumsum()

我得到的结果是一些行填充了数据,而其他行则是NaN。这是我想要的(将B、C列累加到A列的组中):

[[164.93, 114.73, 26.27], [164.89, 114.75, 26....
      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万行)。

假设“A”是用户ID,而这些序列是用户执行的事件序列,在馈入Keras LSTM模型之前需要进行分组。 - scc
你能否给出你的数据框中 7 行的预期输出? - Kenan
@ksooklall,我刚刚完成了。感谢您的任何帮助。 - scc
1个回答

2

看起来你并不是在进行算术求和,更像是在沿着轴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'})


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接