## Get sum of some rows of a pd.DataFrame

I have a pandas dataframe look like below, and I would like to calculate sum of some rows. I want to calculate this way:

I want to get the sum of rows in column 1 from "Glass" to "Tables" and then the sum of rows in column1 from "Tables" to something else, because glass to table is one category and there are other categories.

Because I have several of these sheets and for each one I don't know how many items are under each category, so I cannot sum rows by calling row index.

Can anyone help me with this?

0             1
0                     Glass       26,990
1                   Fabrics       30,853
2   Furniture & Accessories       136,022
3                    Tables       132,454
4     Cabinet/Door Hardware       4,872
6                    Fences       5,952
7               Glass Doors       12,571

You can use pd.concat() assuming your categories are defined in a similar manner:

categories = [('Glass','Tables'), ('Tables','Glass Doors')]

pd.concat([df.set_index(0)[c[0]:c[1]].sum() for c in categories], keys=categories)

Returns:

Glass   Tables       1    326319
Tables  Glass Doors  1    155849

Pandas: sum DataFrame rows for given columns, You may use the following syntax to sum each column and row in Pandas What if you want to get the count, rather than the sum, for each column and row in​  You may use the following syntax to sum each column and row in Pandas DataFrame: (1) Sum each column: df.sum(axis=0) (2) Sum each row: df.sum(axis=1) In the next section, you’ll see how to apply the above syntax using a simple example. Steps to Sum each Column and Row in Pandas DataFrame Step 1: Prepare your Data

Assuming the order is already correct:

df.set_index(0)['Glass':'Tables'].sum()

How to Sum each Column and Row in Pandas DataFrame, sum . Parameters. axis{index (0), columns (1)}. Axis for the function to be applied​  How To Sum The First Day S Value Of Each Id In Pandas Quora Python dataframe how to sum row values with groupby stack overflow how to keep inde when sum by columns based on grouped in how to keep inde when sum by columns based on grouped in python pandas dataframe sum geeksforgeeks. Whats people lookup in this blog:

This is what you might do

import pandas as pd
categories = {'Glass': 'Category 1', 'Fabrics': 'Category 1',
'Furniture & Accessories': 'Category 1',
'Tables': 'Category 2', 'Cabinet/Door Hardware': 'Category 2',
'Fences': 'Category 3', 'Glass Doors': 'Category 4'}
df = pd.read_csv('rfile1.csv', converters={0: lambda x: categories[x]})
df
0       1
0  Category 1   26990
1  Category 1   30853
2  Category 1  136022
3  Category 2  132454
4  Category 2    4872
5  Category 3    5952
6  Category 4   12571

df.groupby('0').sum()
sum
0
Category 1  193865
Category 2  137326
Category 3    5952
Category 4   12571

pandas.DataFrame.sum, Now find the sum of all values along the index axis. We are going to skip the NaN values in the calculation of the sum. Pandas dataframe.sum () function return the sum of the values for the requested axis. If the input is index axis then it adds all the values in a column and repeats the same for all the columns and returns a series containing the sum of all the values in each column.

Python, C:\pandas>python example40.py Apple Orange Banana Pear Sum Basket Basket1 10 20 30 40 100 Basket2 7 14 21 28 70 Basket3 5 5 0 0 10  DataFrame.sum(self, axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)[source]¶. Return the sum of the values for the requested axis. This is equivalent to the method numpy.sum. Parameters. axis{index (0), columns (1)}

Calculate sum across rows and columns in Pandas DataFrame , Column And Row Sums In Pandas And Numpy. I feel like I am constantly looking it up, so now it is documented: If you want to do a row sum in  If you have a just a few columns to sum, you can write: df['e'] = df.a + df.b + df.d This creates new column e with the values:. a b c d e 0 1 2 dd 5 8 1 2 3 ee 9 14

Column And Row Sums In Pandas And Numpy, I have a dataframe with 2 columns: "Name" and "Score". Now I want to find the sum of the scores. How can I do this? Pandas dataframe.cumsum() is used to find the cumulative sum value over any axis. Each cell is populated with the cumulative sum of the values seen so far. Syntax: DataFrame.cumsum(axis=None, skipna=True, *args, **kwargs) Parameters: axis : {index (0), columns (1)} skipna : Exclude NA/null values. If an entire row/column is NA, the result will be NA