Skip to content Skip to sidebar Skip to footer

Sum Only Certain Rows In A Given Column Of Pandas Dataframe

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 can sum the first 310 rows in a certain

Solution 1:

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
a12b26
c  37

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')])
a1b2
c    3
Name: x, dtype: int64

Solution 2:

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

Post a Comment for "Sum Only Certain Rows In A Given Column Of Pandas Dataframe"