代码之家  ›  专栏  ›  技术社区  ›  EEPBAH

返回A列的所有值并将其放入B列,直到达到特定值

  •  2
  • EEPBAH  · 技术社区  · 7 年前

        df    max_val = 8
    
        A
        1
        2
        2
        3 
        4
        5
        1
    

        df    max_val = 8
    
        A    B
        1    1
        2    2
        2    2
        3    3
        4    0
        5    0
        1    0
    

    我是这样想的

        def func(x):
            if df['A'].cumsum() <= max_val:
                return x
            else:
              return 0
    

         df['B'] = df['A'].apply(func, axis =1 ) 
    

         df['B'] = func(df['A'])
    
    7 回复  |  直到 7 年前
        1
  •  2
  •   jezrael    7 年前

    Series.where :

    df['B'] = df['A'].where(df['A'].cumsum() <= max_val, 0)
    print (df)
       A  B
    0  1  1
    1  2  2
    2  2  2
    3  3  3
    4  4  0
    5  5  0
    6  1  0
    
        2
  •  1
  •   Divakar    7 年前

    方法#1 np.where

    df['B']= np.where((df.A.cumsum()<=max_val), df.A ,0)
    

    In [145]: df
    Out[145]: 
       A  B
    0  1  1
    1  2  2
    2  2  2
    3  3  3
    4  4  0
    5  5  0
    6  1  0
    

    方法#2 另一个使用 array-initialization

    def app2(df,max_val):
        a = df.A.values
        colB = np.zeros(df.shape[0],dtype=a.dtype)
        idx = np.searchsorted(a.cumsum(),max_val, 'right')
        colB[:idx] = a[:idx]
        df['B'] = colB
    

    运行时测试

    看起来像 @jezrael's pd.where based one

    In [293]: df = pd.DataFrame({'A':np.random.randint(0,9,(1000000))})
    
    In [294]: max_val = 1000000
    
    # @jezrael's soln
    In [295]: %timeit df['B1'] = df['A'].where(df['A'].cumsum() <= max_val, 0)
    100 loops, best of 3: 8.22 ms per loop
    
    # Proposed in this post
    In [296]: %timeit df['B2']= np.where((df.A.cumsum()<=max_val), df.A ,0)
    100 loops, best of 3: 6.45 ms per loop
    
    # Proposed in this post
    In [297]: %timeit app2(df, max_val)
    100 loops, best of 3: 4.47 ms per loop
    
        3
  •  1
  •   BENY    7 年前
    df['B']=[x if x<=8 else 0 for x in df['A'].cumsum()]
    df
    Out[7]: 
       A  B
    0  1  1
    1  2  3
    2  2  5
    3  3  8
    4  4  0
    5  5  0
    6  1  0
    
        4
  •  0
  •   Nenri    7 年前

    为什么不向这样的变量添加值:

    for i in range(len(df)):
        if A<max_val:
            return x
        else:
            return 0
        A=A+df[i]
    
        5
  •  0
  •   00__00__00    7 年前

    import pandas as pd
    A=[1,2,2,3,4,5,1]
    MAXVAL=8
    df=pd.DataFrame(data=A,columns=['A'])
    df['cumsumA']=df['A'].cumsum()
    df['B']=df['cumsumA']*(df['cumsumA']<MAXVAL).astype(int)
    

    然后可以删除“cumsumA”列

        6
  •  0
  •   Brij    7 年前

    下面的操作很好-

    import numpy as np
    max_val = 8
    df['B'] = np.where(df['A'].cumsum() <= max_val , df['A'],0)
    

        7
  •  0
  •   Forrains_459    7 年前

    只是一种方法 .loc :

    df['c'] = df['a'].cumsum()
    df['b'] = df['a']
    df['b'].loc[df['c'] > 8] = 0