【问题标题】:Sum negative row values with previous rows pandas将负行值与前一行 pandas 相加
【发布时间】:2021-04-10 01:29:03
【问题描述】:

我很难找到一种好方法来查找列中的所有负条目并将它们向上移动,将它们与现有条目相加(即从当前条目中减去负条目),直到所有值为正.

重要的是最终数据帧没有负值,并且所有以前的负条目 = 0。此外,该表是重复的,这意味着我需要根据 ID 以及条目(仅对相同 ID 的条目求和)。

基于此处已提供的表格:

现在:

ID Date Entries
1 2013 100
1 2014 0
1 2015 60
1 2016 -30
1 2017 0
1 2018 50
1 2019 0
1 2020 -20
2 2013 100
2 2014 0
2 2015 60
2 2016 -30
2 2017 0
2 2018 50
2 2019 0
2 2020 -20

期望:

ID Date Entries
1 2013 100
1 2014 0
1 2015 30
1 2016 0
1 2017 0
1 2018 30
1 2019 0
1 2020 0
2 2013 100
2 2014 0
2 2015 30
2 2016 0
2 2017 0
2 2018 30
2 2019 0
2 2020 0

【问题讨论】:

    标签: python python-3.x pandas dataframe


    【解决方案1】:

    值数组的直接递归函数

    df = pd.read_csv(io.StringIO("""ID  Date    Entries
    1   2013    100
    1   2014    0
    1   2015    60
    1   2016    -30
    1   2017    0
    1   2018    50
    1   2019    0
    1   2020    -20
    2   2013    100
    2   2014    0
    2   2015    60
    2   2016    -30
    2   2017    0
    2   2018    50
    2   2019    0
    2   2020    -20"""), sep="\t")
    
    def shiftminus(a):
        touch=False
        for i,n in enumerate(a):
            if n<0 and i>0:
                a[i-1] += a[i]
                a[i] = 0
                touch=True
        if touch:
            a = shiftminus(a)
        return a
    
    df["Entries"] = shiftminus(df["Entries"].values)
    
    
    

    输出

     ID  Date  Entries
      1  2013      100
      1  2014        0
      1  2015       30
      1  2016        0
      1  2017        0
      1  2018       30
      1  2019        0
      1  2020        0
      2  2013      100
      2  2014        0
      2  2015       30
      2  2016        0
      2  2017        0
      2  2018       30
      2  2019        0
      2  2020        0
    

    【讨论】:

      【解决方案2】:

      创建组后可以尝试反向cumsum,然后mask:

      s = df['Entries'].gt(0).cumsum()
      u= df['Entries'][::-1].groupby(s).cumsum().mask(df['Entries'].le(0),0)
      out = df.assign(New_Entries=u) # you can assign to the original column too.
      

      print(out)
          ID  Date  Entries  New_Entries
      0    1  2013      100          100
      1    1  2014        0            0
      2    1  2015       60           30
      3    1  2016      -30            0
      4    1  2017        0            0
      5    1  2018       50           30
      6    1  2019        0            0
      7    1  2020      -20            0
      8    2  2013      100          100
      9    2  2014        0            0
      10   2  2015       60           30
      11   2  2016      -30            0
      12   2  2017        0            0
      13   2  2018       50           30
      14   2  2019        0            0
      15   2  2020      -20            0
      

      【讨论】:

      • 很遗憾 cumsum 没有升序参数,但这很可爱,很好。
      • @Manakin 我看了两次以确保它不存在,因为我认为它会在某个地方。好消息是 pandas 保留索引,所以我不必在分配之前反转系列。 :)
      猜你喜欢
      • 2019-10-10
      • 2017-11-23
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      相关资源
      最近更新 更多