【问题标题】:Python pandas dataframe backfill based on two conditions基于两个条件的 Python pandas 数据帧回填
【发布时间】:2020-01-25 23:36:29
【问题描述】:

我有一个这样的数据框:

   Bool   Hour
0  False  12
1  False  24
2  False  12
3  False  24
4  True   12
5  False  24
6  False  12
7  False  24
8  False  12
9  False  24
10 False  12
11 True   24

我想将“Bool”列中的 True 值回填到“Hour”第一次达到“12”时的位置。结果会是这样的:

   Bool   Hour  Result
0  False  12    False
1  False  24    False
2  False  12    True      <- desired backfill
3  False  24    True      <- desired backfill
4  True   12    True
5  False  24    False
6  False  12    False
7  False  24    False
8  False  12    False
9  False  24    False
10 False  12    True      <- desired backfill
11 True   24    True

非常感谢任何帮助!非常感谢!

【问题讨论】:

    标签: python pandas dataframe boolean


    【解决方案1】:

    这有点难实现,这里我们可以使用groupbyidxmax

    s=(~df.Bool&df.Hour.eq(12)).iloc[::-1].groupby(df.Bool.iloc[::-1].cumsum()).transform('idxmax')
    df['result']=df.index>=s.iloc[::-1]
    df
    Out[375]: 
         Bool  Hour  result
    0   False    12   False
    1   False    24   False
    2   False    12    True
    3   False    24    True
    4    True    12    True
    5   False    24   False
    6   False    12   False
    7   False    24   False
    8   False    12   False
    9   False    24   False
    10  False    12    True
    11   True    24    True
    

    【讨论】:

    • 在最后一个False 组没有Truebfill 的边缘情况下,这仍然填充True ;p
    【解决方案2】:

    IIUC,你可以这样做:

    s = df['Bool'].shift(-1)
    df['Result'] = df['Bool'] | s.where(s).groupby(df['Hour'].eq(12).cumsum()).bfill()
    

    输出:

         Bool  Hour  Result
    0   False    12   False
    1   False    24   False
    2   False    12    True
    3   False    24    True
    4    True    12    True
    5   False    24   False
    6   False    12   False
    7   False    24   False
    8   False    12   False
    9   False    24   False
    10  False    12    True
    11   True    24    True
    

    【讨论】:

      【解决方案3】:

      在连续的False 上创建一个组ID s 并将True 与它们分开。通过使用sHour 上的 Groupby 等于 12。使用变换sumcumsum 从每个组的自下而上获取True12 的计数,并在0or 上返回True,其值为Bool

      s = df.Bool.ne(df.Bool.shift()).cumsum()
      s1 = df.where(df.Bool).Bool.bfill()
      g = df.Hour.eq(12).groupby(s)
      df['bfill_Bool'] = (g.transform('sum') - g.cumsum()).eq(0) & s1 | df.Bool
      
      Out[905]:
           Bool  Hour  bfill_Bool
      0   False    12       False
      1   False    24       False
      2   False    12        True
      3   False    24        True
      4    True    12        True
      5   False    24       False
      6   False    12       False
      7   False    24       False
      8   False    12       False
      9   False    24       False
      10  False    12        True
      11   True    24        True
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 2018-09-26
        • 2019-01-25
        • 2019-10-13
        • 2018-01-28
        • 1970-01-01
        • 2019-10-22
        相关资源
        最近更新 更多