pandas 左对齐熊猫滚动对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38055632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Left-align a pandas rolling object
提问by Alex
Using pandas 0.18.1, I'd like to take the rolling average of a one-column dataframe. Since version 0.18.0, this is done with rolling() objects. The default for these rolling objects is to be right-justified. There is a boolean argument you can pass, center=True, to align the rolling object to the center value, but there doesn't seem to be a way to left-align it. Here's an example:
使用 pandas 0.18.1,我想取一列数据框的滚动平均值。从 0.18.0 版本开始,这是通过滚动()对象完成的。这些滚动对象的默认设置是右对齐。您可以传递一个布尔参数,center=True,将滚动对象与中心值对齐,但似乎没有办法将其左对齐。下面是一个例子:
df = pandas.DataFrame({'A': [2,3,6,8,20, 27]})
df
A
0 2
1 3
2 6
3 8
4 20
5 27
The standard method automatically aligns to the right, so there's no value at the first two indecies with a window of size three:
标准方法会自动向右对齐,因此在窗口大小为 3 的前两个 indecies 处没有值:
df.rolling(window=3).mean()
A
0 NaN
1 NaN
2 3.666667
3 5.666667
4 11.333333
5 18.333333
We can center-align the operation like this:
我们可以像这样居中对齐操作:
df.rolling(window=3).mean(center=True)
A
0 NaN
1 3.666667
2 5.666667
3 11.333333
4 18.333333
5 NaN
But what I'm looking for is this:
但我要找的是这个:
df.rolling(3).mean()
A
0 3.666667
1 5.666667
2 11.333333
3 18.333333
4 NaN
5 NaN
I can accomplish this by doing it with the default right alignment, and then re-indexing it, or by reversing the order of the rows and then doing it "right-aligned" but these are work-arounds for what should be a straight-forward operation.
我可以通过使用默认的右对齐来实现这一点,然后重新索引它,或者通过反转行的顺序然后“右对齐”来完成,但这些是应该是直线的变通方法 -前向操作。