Summing across rows of Pandas Dataframe

前端 未结 1 2173
时光取名叫无心
时光取名叫无心 2021-02-20 18:30

I have a DataFrame of records that looks something like this:

stocks = pd.Series([\'A\', \'A\', \'B\', \'C\', \'C\'], name = \'stock\')
positions = pd.Series([ 1         


        
相关标签:
1条回答
  • 2021-02-20 19:12

    Step 1. Use [['positions']] instead of ['positions']:

    In [30]: df2 = df.groupby(['stock','same1','same2'])[['positions']].sum()
    
    In [31]: df2 
    Out[31]: 
    
                       positions
    stock same1 same2               
    A     AA    AAA          300 
    B     BB    BBB          300 
    C     CC    CCC          900 
    

    Step 2. And then use reset_index to move the index back to the column

    In [34]: df2.reset_index()
    Out[34]: 
      stock same1 same2  positions
    0     A    AA   AAA        300 
    1     B    BB   BBB        300 
    2     C    CC   CCC        900
    

    EDIT

    Seems my method is not so good.

    Thanks to @Andy and @unutbu , you can achieve your goal by more elegant ways:

    method 1:

    df.groupby(['stock', 'same1', 'same2'])['positions'].sum().reset_index()
    

    method 2:

    df.groupby(['stock', 'same1', 'same2'], as_index=False)['positions'].sum()
    
    0 讨论(0)
提交回复
热议问题