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 negative and positive values using GroupBy in Pandas
When working with grouped data in Pandas, you may need to calculate separate sums for positive and negative values within each group. This is useful for analyzing temperature data, financial gains/losses, or any dataset with both positive and negative values.
Creating the DataFrame
First, let's create a DataFrame with temperature data containing both positive and negative values ?
import pandas as pd
# Create DataFrame with temperature data
dataFrame = pd.DataFrame({
'Place': ['Chicago', 'Denver', 'Atlanta', 'Chicago', 'Dallas', 'Denver', 'Dallas', 'Atlanta'],
'Temperature': [-2, 30, -5, 10, 30, -5, 20, -10]
})
print(dataFrame)
Place Temperature
0 Chicago -2
1 Denver 30
2 Atlanta -5
3 Chicago 10
4 Dallas 30
5 Denver -5
6 Dallas 20
7 Atlanta -10
Defining Helper Functions
We need two functions to separate positive and negative values ?
import pandas as pd
def plus(val):
return val[val > 0].sum()
def minus(val):
return val[val < 0].sum()
# Test the functions
test_data = pd.Series([-2, 10, -5])
print("Positive sum:", plus(test_data))
print("Negative sum:", minus(test_data))
Positive sum: 10 Negative sum: -7
Grouping and Aggregating
Now we can group by Place and apply both functions to get separate sums ?
import pandas as pd
# Create DataFrame
dataFrame = pd.DataFrame({
'Place': ['Chicago', 'Denver', 'Atlanta', 'Chicago', 'Dallas', 'Denver', 'Dallas', 'Atlanta'],
'Temperature': [-2, 30, -5, 10, 30, -5, 20, -10]
})
# Define functions
def plus(val):
return val[val > 0].sum()
def minus(val):
return val[val < 0].sum()
# Group by Place and apply both functions
groupRes = dataFrame.groupby('Place')
result = groupRes['Temperature'].agg([('negTemp', minus), ('posTemp', plus)])
print(result)
negTemp posTemp
Place
Atlanta -15 0
Chicago -2 10
Dallas 0 50
Denver -5 30
Alternative Approach Using Lambda Functions
You can also use lambda functions directly in the aggregation ?
import pandas as pd
dataFrame = pd.DataFrame({
'Place': ['Chicago', 'Denver', 'Atlanta', 'Chicago', 'Dallas', 'Denver', 'Dallas', 'Atlanta'],
'Temperature': [-2, 30, -5, 10, 30, -5, 20, -10]
})
# Using lambda functions directly
result = dataFrame.groupby('Place')['Temperature'].agg([
('negTemp', lambda x: x[x < 0].sum()),
('posTemp', lambda x: x[x > 0].sum())
])
print(result)
negTemp posTemp
Place
Atlanta -15 0
Chicago -2 10
Dallas 0 50
Denver -5 30
Key Points
| Aspect | Description |
|---|---|
| Filtering | Use val[val > 0] for positive, val[val < 0] for negative |
| Aggregation |
.agg() allows multiple functions with custom names |
| Result | Returns DataFrame with separate columns for each aggregation |
Conclusion
Use groupby().agg() with custom functions to separate positive and negative value sums. This approach is efficient for analyzing data with mixed positive/negative values across different categories.
