本文介绍了以向量化方式计算大 pandas 中特定连续相等值的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下熊猫DataFrame:

Let's say we have the following pandas DataFrame:

In [1]:
import pandas as pd
import numpy as np

df = pd.DataFrame([0, 1, 0, 0, 1, 1, 0, 1, 1, 1], columns=['in'])
df
Out[1]: 
   in
0   0
1   1
2   0
3   0
4   1
5   1
6   0
7   1
8   1
9   1

如何以向量化的方式计算大熊猫中连续的数量?我想要这样的结果:

How to count the number of consecutive ones in a vectorized way in pandas? I would like to have a result like this:

   in  out
0   0    0
1   1    1
2   0    0
3   0    0
4   1    1
5   1    2
6   0    0
7   1    1
8   1    2
9   1    3

类似于矢量化求和操作的操作,该操作会在特定条件下重置.

Something like a vectorized cumsum operation that resets on a specific condition.

推荐答案

您可以执行以下操作(信用信息转到:):

You can do something like this(credit goes to: how to emulate itertools.groupby with a series/dataframe?):

>>> df['in'].groupby((df['in'] != df['in'].shift()).cumsum()).cumsum()
0    0
1    1
2    0
3    0
4    1
5    2
6    0
7    1
8    2
9    3
dtype: int64

这篇关于以向量化方式计算大 pandas 中特定连续相等值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 17:22