- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the sum of a specific column of a dataframe in Pandas Python?
Sometimes, it may be required to get the sum of a specific column. This is where the ‘sum’ function can be used.
The column whose sum needs to be computed can be passed as a value to the sum function. The index of the column can also be passed to find the sum.
Let us see a demonstration of the same −
Example
import pandas as pd my_data = {'Name':pd.Series(['Tom','Jane','Vin','Eve','Will']),'Age':pd.Series([45, 67, 89, 12, 23]),'value':pd.Series([8.79,23.24,31.98,78.56,90.20]) } print("The dataframe is :") my_df = pd.DataFrame(my_data) print(my_df) print("The sum of 'age' column is :") print(my_df.sum(1))
Output
The dataframe is : Name Age value 0 Tom 45 8.79 1 Jane 67 23.24 2 Vin 89 31.98 3 Eve 12 78.56 4 Will 23 90.20 The sum of 'age' column is : 0 53.79 1 90.24 2 120.98 3 90.56 4 113.20 dtype: float64
Explanation
The required libraries are imported, and given alias names for ease of use.
Dictionary of series consisting of key and value is created, wherein a value is actually a series data structure.
This dictionary is later passed as a parameter to the ‘Dataframe’ function present in the ‘pandas’ library
The dataframe is printed on the console.
We are looking at computing the sum of the ‘Age’ column.
The name of the column whose sum needs to be computed is passed as a parameter to the ‘sum’ function.
The sum is printed on the console.
- Related Articles
- How to get the mean of a specific column in a dataframe in Python?
- Python - Sum only specific rows of a Pandas Dataframe
- Python – Group and calculate the sum of column values of a Pandas DataFrame
- How to get the list of column headers from a Pandas DataFrame?
- How to sort a column of a Pandas DataFrame?
- Python - Calculate the variance of a column in a Pandas DataFrame
- How to find the standard deviation of specific columns in a dataframe in Pandas Python?
- Python - How to select a column from a Pandas DataFrame
- Python - Calculate the standard deviation of a column in a Pandas DataFrame
- Python - Calculate the mean of column values of a Pandas DataFrame
- Python - Calculate the median of column values of a Pandas DataFrame
- Python - Calculate the count of column values of a Pandas DataFrame
- Python - Calculate the maximum of column values of a Pandas DataFrame
- Python - Calculate the minimum of column values of a Pandas DataFrame
- Python Pandas - Display specific number of rows from a DataFrame
