如何根据其他列的值将移动平均线包括在熊猫中

时间:2020-03-16 09:07:44

标签: python pandas moving-average

我正在尝试在以下数据帧上计算移动平均线,但无法将结果重新添加到数据帧中
数据框为:(移动平均值显示在括号中)

Key1 Key2 Value MovingAverage  
  1    2    1       (Nan)
  1    7    2       (Nan)
  1    8    3       (Nan)
  2    5    1       (Nan)
  2    3    2       (Nan)
  2    2    3       (Nan)
  3    7    1       (Nan)
  3    5    2       (Nan)
  3    8    3       (Nan)
  4    7    1       (1.33)
  4    2    2        (2)
  4    9    3       (Nan)
  5    8    1       (2.33)
  5    3    2       (Nan)
  5    9    3       (Nan)
  6    2    1        (2)
  6    7    2       (1.33)
  6    9    3        (3)

代码为:

import pandas as pd
d = {'Key1':[1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6], 'Key2':[2,7,8,5,3,2,7,5,8,7,2,9,8,3,9,2,7,9],'Value':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3]}
df = pd.DataFrame(d)
print(df)
MaDf = df.groupby(['Key2'])['Value'].rolling(window=3).mean().to_frame('mean')
print (MaDf) 

如果运行代码,它将基于'Key2'和'Value'正确计算移动平均线,但我找不到正确将其重新插入到原始数据帧(df)的方法

2 个答案:

答案 0 :(得分:4)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <input class="form-control" type="text" id="name" name="name" value="Mark"/> <input class="form-control" type="text" id="doc" name="doc" value= "111"/> <input class="form-control" type="text" id="cell" name="cell" value= "617"/>Series.reset_index除去MultiIndex的第一级,以按第二级对齐:

drop=True

如果可以使用默认的df['mean'] = (df.groupby('Key2')['Value'] .rolling(window=3) .mean() .reset_index(level=0, drop=True)) print (df) Key1 Key2 Value mean 0 1 2 1 NaN 1 1 7 2 NaN 2 1 8 3 NaN 3 2 5 1 NaN 4 2 3 2 NaN 5 2 2 3 NaN 6 3 7 1 NaN 7 3 5 2 NaN 8 3 8 3 NaN 9 4 7 1 1.333333 10 4 2 2 2.000000 11 4 9 3 NaN 12 5 8 1 2.333333 13 5 3 2 NaN 14 5 9 3 NaN 15 6 2 1 2.000000 16 6 7 2 1.333333 17 6 9 3 3.000000 ,请使用Series.sort_index

RangeIndex

答案 1 :(得分:2)

df['mean'] = df.groupby(['Key2'])['Value'].rolling(window=3).mean().values