Sum only certain rows in a given column of pandas dataframe

前端 未结 2 1026
孤街浪徒
孤街浪徒 2020-12-11 11:13

I can sum the first 310 rows in a 5 column pandas dataframe and get a tidy summary by using:

df.[0:310].sum

Is there an easy way whereby I

相关标签:
2条回答
  • 2020-12-11 11:33

    You need something like this:

    import pandas as pd
    data = {'x':[1,2,3,4,5], 'y':[2,5,7,9,11], 'z':[2,6,7,3,4]}
    df=pd.DataFrame(data)
    

    Use list of columns along with rows:

    df.loc[0:310][['x','z']].sum()
    

    output:

    x    15
    z    22
    dtype: int64
    
    0 讨论(0)
  • 2020-12-11 11:51

    I think need DataFrame.iloc for select rows by positions with get_indexer for positions of columns by names:

    #data borrowed from Akshay Nevrekar answer, but changed index values
    data = {'x':[1,2,3,4,5], 
            'y':[2,5,7,9,11], 
            'z':[2,6,7,3,4]}
    df=pd.DataFrame(data, index=list('abcde'))
    print (df)
       x   y  z
    a  1   2  2
    b  2   5  6
    c  3   7  7
    d  4   9  3
    e  5  11  4
    
    a = df.iloc[:3, df.columns.get_indexer(['x','z'])].sum()
    

    What is same as:

    a = df.iloc[:3, [0,2]].sum()
    
    print (a)
    x     6
    z    15
    dtype: int64
    

    Detail:

    print (df.iloc[:3, df.columns.get_indexer(['x','z'])])
       x  z
    a  1  2
    b  2  6
    c  3  7
    

    If want only one column use get_loc for position:

    b = df.iloc[:3, df.columns.get_loc('x')].sum()
    

    What is same as:

    b = df.iloc[:3, 0].sum()
    
    print (b)
    6
    

    Detail:

    print (df.iloc[:3, df.columns.get_loc('x')])
    a    1
    b    2
    c    3
    Name: x, dtype: int64
    
    0 讨论(0)
提交回复
热议问题