简体   繁体   English

Pandas:条件滚动 window 由另一个列元素?

[英]Pandas: Conditional Rolling window by another column element?

I have a dataframe with dates, id's and values.我有一个带有日期、ID 和值的 dataframe。

For example:例如:

date        id        value
2016-08-28   A          1
2016-08-28   B          1
2016-08-29   C          2
2016-09-02   B          0
2016-09-03   A          3
2016-09-06   C          1
2017-01-15   B          2
2017-01-18   C          3
2017-01-18   A          2

I want to apply a rolling mean by element, stating one after, so that the result would be like:我想逐个元素应用滚动平均值,然后声明一个,这样结果就会像:

date        id        value    rolling_mean
2016-08-28   A          1           NaN
2016-08-28   B          1           NaN
2016-08-29   C          2           NaN
2016-09-02   B          0           0.5
2016-09-03   A          3           2.0
2016-09-06   C          1           1.5
2017-01-15   B          2           1.0
2017-01-18   C          3           2.0
2017-01-18   A          2           2.5

The closest I've come to this was:我最接近的是:

grouped = df.groupby(["id", "value"])
df["rolling_mean"] = grouped["value"].shift(1).rolling(window = 2).mean()

But this gives me the wrong values back, as it keeps the order with the remaining elements.但这给了我错误的值,因为它保持了其余元素的顺序。

Any ideia?有什么想法吗?

Thank you in advance,先感谢您,

You can just groupby id and use transform :您可以仅 groupby id并使用transform

df['rolling_mean'] = df.groupby('id')['value'].transform(lambda x: x.rolling(2).mean())

Output: Output:

         date id  value  rolling_mean
0  2016-08-28  A      1           NaN
1  2016-08-28  B      1           NaN
2  2016-08-29  C      2           NaN
3  2016-09-02  B      0           0.5
4  2016-09-03  A      3           2.0
5  2016-09-06  C      1           1.5
6  2017-01-15  B      2           1.0
7  2017-01-18  C      3           2.0
8  2017-01-18  A      2           2.5

Fix your code with groupby with id使用带有 id 的groupby修复您的代码

grouped = df.groupby(["id"])
df['rolling_mean']=grouped["value"].rolling(window = 2).mean().reset_index(level=0,drop=True)
df
Out[67]: 
        date id  value  rolling_mean
0 2016-08-28  A      1           NaN
1 2016-08-28  B      1           NaN
2 2016-08-29  C      2           NaN
3 2016-09-02  B      0           0.5
4 2016-09-03  A      3           2.0
5 2016-09-06  C      1           1.5
6 2017-01-15  B      2           1.0
7 2017-01-18  C      3           2.0
8 2017-01-18  A      2           2.5

Like this:像这样:

df['rolling_mean'] = df.groupby('id')['value'].rolling(2).mean().reset_index(0,drop=True).sort_index()

Output: Output:

         date id  value  rolling_mean
0  2016-08-28  A      1           nan
1  2016-08-28  B      1           nan
2  2016-08-29  C      2           nan
3  2016-09-02  B      0          0.50
4  2016-09-03  A      3          2.00
5  2016-09-06  C      1          1.50
6  2017-01-15  B      2          1.00
7  2017-01-18  C      3          2.00
8  2017-01-18  A      2          2.50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动条件熊猫DataFrame列 - Rolling Conditional Pandas DataFrame Column 根据另一列中指定的 window 大小在 pandas 中创建滚动 windows - Create rolling windows in pandas based on window size specified in another column pandas df列中的交替有条件滚动计数 - alternating, conditional rolling count in pandas df column Pandas:按组条件滚动计数,计算当前观察出现在另一列的次数 - Pandas: conditional rolling count by group, counting the number of times current observation appeared in another column Pandas:如何计算一个列(按日期分组)上的滚动 window 并计算另一列的不同值? - Pandas: how to calculate a rolling window over one column (grouped by date) and count distinct values of another column? 如何在具有其他条件的熊猫中创建滚动窗口 - How to create a rolling window in pandas with another condition 计算熊猫列上的滚动窗口加权平均值 - Calculate a rolling window weighted average on a Pandas column pandas 滚动 window 聚合字符串列 - pandas rolling window aggregating string column 熊猫:如何在滚动窗口中选择一列 - Pandas: How to select a column in rolling window Pandas:在滚动 window 中找到最大值,并返回最大值所在行的另一列的总和以及后续四行 - Pandas: Find max in rolling window and return sum of another column for the row of the max and proceeding four rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM