Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of specific rows in a dataframe (Pandas)

I'm given a set of the following data:

week  A      B      C      D      E
1     243    857    393    621    194
2     644    576    534    792    207
3     946    252    453    547    436
4     560    100    864    663    949
5     712    734    308    385    303

I’m asked to find the sum of each column for specified rows/a specified number of weeks, and then plot those numbers onto a bar chart to compare A-E.

Assuming I have the rows I need (e.g. df.iloc[2:4,:]), what should I do next? My assumption is that I need to create a mask with a single row that includes the sum of each column, but I'm not sure how I go about doing that.

I know how to do the final step (i.e. .plot(kind='bar'), I just need to know what the middle step is to obtain the sums I need.

like image 607
Fares K. A. Avatar asked May 07 '18 16:05

Fares K. A.


1 Answers

You can use for select by positions iloc, sum and Series.plot.bar:

df.iloc[2:4].sum().plot.bar()

graph1

Or if want select by names of index (here weeks) use loc:

df.loc[2:4].sum().plot.bar()

graph2

Difference is iloc exclude last position:

print (df.loc[2:4])
        A    B    C    D    E
week                         
2     644  576  534  792  207
3     946  252  453  547  436
4     560  100  864  663  949

print (df.iloc[2:4])
        A    B    C    D    E
week                         
3     946  252  453  547  436
4     560  100  864  663  949

And if need also filter columns by positions:

df.iloc[2:4, :4].sum().plot.bar()  

And by names (weeks):

df.loc[2:4, list('ABCD')].sum().plot.bar()
like image 75
jezrael Avatar answered Oct 11 '22 05:10

jezrael