【问题标题】:Generating a tuple of indexes based on a sequence of values in a pandas DataFrame根据 pandas DataFrame 中的值序列生成索引元组
【发布时间】:2020-05-11 17:51:11
【问题描述】:

这是我上一个问题的后续:Finding the index of rows based on a sequence of values in a column of pandas DataFrame

我想得到一个索引非常糟糕的元组列表,然后是第一次出现“坏”的索引:

import random

df = pd.DataFrame({
    'measure': [random.randint(0,10) for _ in range(0,20)],
})

df['status'] = df.apply(
    lambda x: 'good' if x['measure'] > 4 else 'very bad' if x['measure'] < 2  else 'bad',
    axis=1)

这是数据框:

    measure    status
0         8      good
1         8      good
2         0  very bad
3         5      good
4         2       bad
5         3       bad
6         9      good
7         9      good
8        10      good
9         5      good
10        1  very bad
11        7      good
12        7      good
13        6      good
14        5      good
15       10      good
16        3       bad
17        0  very bad
18        3       bad

我怎样才能得到这样组合的元组?

[(2,4), (10,16), (17,18)]

【问题讨论】:

    标签: python python-3.x pandas dataframe


    【解决方案1】:

    IIUC,你可以试试:

    # filters only rows with bad and very bad
    m = df[df['status'].isin(['bad','very bad'])] 
    
    # check id current row is very bad and next row is bad
    c = m['status'].eq('very bad') & m['status'].shift(-1).eq('bad')
    
    # if true return next row as true too and get only index values
    idx = m[c|c.shift()].index
    
    # convert every 2 items into a tuple
    res = [*zip(idx[::2],idx[1::2])]
    
    

    [(2, 4), (10, 16), (17, 18)]
    

    【讨论】:

    • 谢谢!您能否就您如何到达这里添加一些评论?
    • @MehdiZare 用 cmets 编辑
    • 你的 cmets 会说话,但我没有翻译成代码的智力。 +1
    • @r.ook 我不这么认为朋友。我已经看到你回答了,我可以保证你的才智:-) bdw 如果你需要更多解释,请告诉我。
    • @anky 如果您能解释第 3 行如何正确获取索引,那就太好了! idx = m[c|c.shift()].index
    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多