将单个数据帧值与同一列中的前10个进行比较 [英] Comparing single dataframe value to previous 10 in same column

查看:143
本文介绍了将单个数据帧值与同一列中的前10个进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据框中,我想计算过去10天的价格是多于今天的价格。结果将如下所示:

In a dataframe, I would like to count how many of the prices from the previous 10 days are greater than today's price. Result would look like this:

price   ct>prev10
50.00   
51.00   
52.00   
50.50   
51.00   
50.00   
50.50   
53.00   
52.00   
49.00   
51.00   3

我已经看到这个帖子由帝斯曼回答,但是要求是不同的,因为比较的基础是一个静态数字而不是当前行:

I have seen this post answered by DSM, but the requirement was different in that the base for comparison was a static number as opposed to the current row:

使用pd.rolling_sum()实现countif

当然,我想在没有循环访问1x1的情况下执行此操作。相当多的困惑 - 提前感谢任何建议。

Of course I would like to do this without looping through 1x1. Pretty much stumped - thanks in advance for any advise.

推荐答案

您可以在系列上使用 rolling_apply 函数。我使用的窗口长度为5,因为样本数据很小,但是您可以轻松地更改它。

You can use a rolling_apply function on the series. I used a window length of 5 given the small size of the sample data, but you can easily change it.

lambda 函数计算滚动组中的项目数(不包括最后一项)大于最后一项。

The lambda function counts the number of items in the rolling group (excluding the last item) is greater than the last item.

df = pd.DataFrame({'price': [50, 51, 52, 50.5, 51, 50, 50.5, 53, 52, 49, 51]})

window = 5  # Given that sample data only contains 11 values.
df['price_count'] = pd.rolling_apply(df.price, window, 
                                     lambda group: sum(group[:-1] > group[-1]))
>>> df
    price  price_count
0    50.0          NaN
1    51.0          NaN
2    52.0          NaN
3    50.5          NaN
4    51.0            1
5    50.0            4
6    50.5            2
7    53.0            0
8    52.0            1
9    49.0            4
10   51.0            2

在上面的示例中,第一组是索引值为0-4的价格。您可以看到发生了什么:

In the example above, the first group is the prices with index values 0-4. You can see what is happening with:

group = df.price[:window].values
>>> group
array([ 50. ,  51. ,  52. ,  50.5,  51. ])

现在,将以前的四个价格与当前价格进行比较:

Now, do your comparison of the previous four prices to the current price:

>>> group[:-1] > group[-1]
array([False, False,  True, False], dtype=bool)

然后,您只是求和布尔值:

Then, you are just summing the boolean values:

>>> sum(group[:-1] > group[-1])
1

这是在索引4中放入第一个关闭窗口的值。

This is the value that gets put into the first closing window at index 4.

这篇关于将单个数据帧值与同一列中的前10个进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆