
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Sum negative and positive values using GroupBy in Pandas
Let us see how to find the sum of negative and positive values. At first, create a dataframe with positive and negative values −
dataFrame = pd.DataFrame({'Place': ['Chicago', 'Denver', 'Atlanta', 'Chicago', 'Dallas', 'Denver','Dallas', 'Atlanta'], 'Temperature': [-2, 30, -5, 10, 30, -5, 20, -10]})
Next, use groupby to group on the basis of Place column −
groupRes = dataFrame.groupby(dataFrame['Place'])
Use lambda function to return the positive and negative values. We have also added the positive and negative values individually −
# lambda function def plus(val): return val[val > 0].sum() def minus(val): return val[val < 0].sum()
Example
Following is the complete code −
import pandas as pd # create a DataFrame with temperature in celsius dataFrame = pd.DataFrame({'Place': ['Chicago', 'Denver', 'Atlanta', 'Chicago', 'Dallas', 'Denver','Dallas', 'Atlanta'], 'Temperature': [-2, 30, -5, 10, 30, -5, 20, -10]}) print(dataFrame) # using groupby to group on the basis of place groupRes = dataFrame.groupby(dataFrame['Place']) # lambda function def plus(val): return val[val > 0].sum() def minus(val): return val[val < 0].sum() print(groupRes['Temperature'].agg([('negTemp', minus), ('posTemp', plus)]))
Output
This will produce the following code −
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 negTemp posTemp Place Atlanta -15 0 Chicago -2 10 Dallas 0 50 Denver -5 30
- Related Articles
- Python - Replace negative values with latest preceding positive value in Pandas DataFrame
- Sum a negative number (negative and positive digits) - JavaScript
- How to Groupby values count on the Pandas DataFrame
- Display the sum of positive and negative values from a column in separate columns with MySQL
- How to print positive and negative infinity values in JavaScript?
- Python - How to reset index after Groupby pandas?
- Write a positive and negative integer whose sum is $-$7.
- Find the Pairs of Positive Negative values in an Array using C++\n
- How to create bar plot with positive and negative values in R?
- Write a positive and a negative integer whose sum is $-7$.
- Lambda expression in Python to rearrange positive and negative numbers
- Java Program to convert positive int to negative and negative to positive
- Converting boolean values to positive or negative sign in MySQL?
- Test array values for positive or negative infinity in Numpy
- Count positive and negative numbers in a list in Python program

Advertisements