python - 如何很好地测量 Pandas 数据框中相同数据的运行

标签 python pandas

我想给一个函数一个任意的数据框、日期索引和列,并要求它返回有多少个连续的前行(包括它自己)具有相同的值。我已经能够使我的大部分 pandas 代码保持矢量化。努力思考我如何才能干净利落地做到这一点。

下面是一个小玩具数据集和我希望从函数中得到的输出示例。

            bar     foo
2016-06-01  False   True
2016-06-02  True    False
2016-06-03  True    True
2016-06-06  True    False
2016-06-07  False   False
2016-06-08  True    False
2016-06-09  True    False
2016-06-10  False   True
2016-06-13  False   True
2016-06-14  True    True


import pandas as pd

rng = pd.bdate_range('6/1/2016', periods=10)
cola = [True, False, True, False, False, False,False, True, True, True]
colb = [False, True, True, True, False, True, True, False, False, True]

d = {'foo':pd.Series(cola, index =rng), 'bar':pd.Series(colb, index=rng)}
df = pd.DataFrame(d)    

"""
consec('foo','2016-06-09') => 4  # it's the fourth  continuous 'False' in a row
consec('foo', '2016-06-08') => 3  # It's the third continuous'False' in a row
consec('bar', '2016-06-02') => 1  # It's the first continuou true in a row
consec('foo', '2016-06-14') => 3  # It's the third continuous True
"""

==================

我最终使用了下面的 itertools-answer,稍作改动,因为它正是我想要的(比我原来的问题规范稍微复杂一些)。感谢您提出许多建议。

rng = pd.bdate_range('6/1/2016', periods=100)
cola = [True, False, True, False, False, False,False, True, True, True]*10
colb = [False, True, True, True, False, True, True, False, False, True]*10

d = {'foo':pd.Series(cola, index =rng), 'bar':pd.Series(colb, index=rng)}
df2 = pd.DataFrame(d)    

def make_new_col_of_consec(df,col_list):
    for col_name in col_list:
        lst = []
        for state, repeat_values in itertools.groupby(df1[col_name]):
            if state == True:
                lst.extend([i+1 for i,v in enumerate(repeat_values)])
            elif state == False:
                lst.extend([0 for i,v in enumerate(repeat_values)])
        df1[col_name + "_consec"] = lst
    return df


print make_new_col_of_consec(df1,["bar","foo"])

输出如下:

              bar    foo  bar_consec  foo_consec
2016-06-01  False   True           0           1
2016-06-02   True  False           1           0
2016-06-03   True   True           2           1
2016-06-06   True  False           3           0
2016-06-07  False  False           0           0
2016-06-08   True  False           1           0
2016-06-09   True  False           2           0
2016-06-10  False   True           0           1
2016-06-13  False   True           0           2
2016-06-14   True   True           1           3
2016-06-15  False   True           0           4
2016-06-16   True  False           1           0
2016-06-17   True   True           2           1
2016-06-20   True  False           3           0
2016-06-21  False  False           0           0
2016-06-22   True  False           1           0

最佳答案

这是另一种方法,它为每一行创建一个具有相关连续计数的新列。我在数据框有 10000 行并且耗时 24 毫秒时对此进行了测试。它使用来自 itertoolsgroupby .它利用了每当键值(在本例中为 foobar 更改时都会创建一个中断的事实,因此我们可以从那里使用索引。

rng = pd.bdate_range('6/1/2016', periods=10000)
cola = [True, False, True, False, False, False,False, True, True, True]*1000
colb = [False, True, True, True, False, True, True, False, False, True]*1000

d = {'foo':pd.Series(cola, index =rng), 'bar':pd.Series(colb, index=rng)}
df1 = pd.DataFrame(d)    

def make_new_col_of_consec(df,col_list):
    for col_name in col_list:
        lst = []
        for state, repeat_values in itertools.groupby(df1[col_name]):
            lst.extend([i+1 for i,v in enumerate(repeat_values)])
        df1[col_name + "_consec"] = lst
    return df


print make_new_col_of_consec(df1,["bar","foo"])

输出:

              bar    foo  bar_consec  foo_consec
2016-06-01  False   True           1           1
2016-06-02   True  False           1           1
2016-06-03   True   True           2           1
2016-06-06   True  False           3           1
2016-06-07  False  False           1           2
2016-06-08   True  False           1           3
...
[10000 rows x 4 columns]
10 loops, best of 3: 24.1 ms per loop

关于python - 如何很好地测量 Pandas 数据框中相同数据的运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38123904/

相关文章:

python - 如何使用python计算csv文件分组项目中特定项目的出现次数

python - 如何测试多个变量与单个值的相等性?

python - 如何在 Django 中执行 MySQL 的连接查询?

python - 如何干净地写__getitem__?

python - Pyinstaller 错误 xfd 模块

python - Pandas Series.from_records?

python - 如何使用 Pandas 删除第一行?

python - 如何在 pandas 上单独计算特征重复(或 Ridit 特征工程)

python - get_dummies 分割字符

python - pandas groupby 方法实际上是如何工作的?