Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Sum only specific rows of a Pandas Dataframe
To sum only specific rows in a Pandas DataFrame, use the loc[] method to select specific row ranges and columns. The loc[] method allows you to specify both row indices and column names for precise data selection.
Let's start by creating a DataFrame with product inventory data ?
import pandas as pd
# Create DataFrame with product inventory data
dataFrame = pd.DataFrame({
"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],
"Opening_Stock": [300, 700, 1200, 1500],
"Closing_Stock": [200, 500, 1000, 900]
})
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
Product Opening_Stock Closing_Stock
0 SmartTV 300 200
1 ChromeCast 700 500
2 Speaker 1200 1000
3 Earphone 1500 900
Summing Specific Rows
To sum only the first two rows (indices 0 and 1) across specific columns, use loc[0:1, ["Opening_Stock", "Closing_Stock"]] and add the sum(axis=1) method ?
import pandas as pd
dataFrame = pd.DataFrame({
"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],
"Opening_Stock": [300, 700, 1200, 1500],
"Closing_Stock": [200, 500, 1000, 900]
})
# Sum specific rows (0 and 1) for specific columns
dataFrame['Sum_Result'] = dataFrame.loc[0:1, ["Opening_Stock", "Closing_Stock"]].sum(axis=1)
print("DataFrame after summing specific rows:")
print(dataFrame)
DataFrame after summing specific rows:
Product Opening_Stock Closing_Stock Sum_Result
0 SmartTV 300 200 500.0
1 ChromeCast 700 500 1200.0
2 Speaker 1200 1000 NaN
3 Earphone 1500 900 NaN
Summing Different Row Ranges
You can also sum different row ranges by changing the indices in loc[] ?
import pandas as pd
dataFrame = pd.DataFrame({
"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],
"Opening_Stock": [300, 700, 1200, 1500],
"Closing_Stock": [200, 500, 1000, 900]
})
# Sum rows 2 and 3 (Speaker and Earphone)
dataFrame['Sum_Last_Two'] = dataFrame.loc[2:3, ["Opening_Stock", "Closing_Stock"]].sum(axis=1)
print("Summing last two rows:")
print(dataFrame)
Summing last two rows:
Product Opening_Stock Closing_Stock Sum_Last_Two
0 SmartTV 300 200 NaN
1 ChromeCast 700 500 NaN
2 Speaker 1200 1000 2200.0
3 Earphone 1500 900 2400.0
Key Points
- Use
loc[start_row:end_row, ["col1", "col2"]]to select specific rows and columns - Add
sum(axis=1)to sum across columns (horizontally) - Rows not included in the range will show
NaNvalues - The range is inclusive on both ends (0:1 includes both row 0 and row 1)
Conclusion
The loc[] method with sum(axis=1) provides precise control for summing specific rows and columns in a DataFrame. This approach is useful for creating calculated fields based on subsets of your data.
