
- 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 Pandas - Filtering columns from a DataFrame on the basis of sum
To filter on the basis of sum of columns, we use the loc() method. Here, in our example, we sum the marks of each student to get the student column with marks above 400 i.e. 80%.
At first, create a DataFrame with student records. We have marks records of 3 students i.e 3 columns −
dataFrame = pd.DataFrame({ 'Jacob_Marks': [95, 90, 75, 85, 88],'Ted_Marks': [60, 50, 65, 85, 70],'Jamie_Marks': [77, 76, 65, 45, 50]})
Filtering on the basis of columns. Fetching student with total marks above 400 −
dataFrame = dataFrame.loc[:, dataFrame.sum(axis=0) > 400]
Example
Following is the complete code −
import pandas as pd # create a dataframe with 3 columns dataFrame = pd.DataFrame({ 'Jacob_Marks': [95, 90, 75, 85, 88],'Ted_Marks': [60, 50, 65, 85, 70],'Jamie_Marks': [77, 76, 65, 45, 50]}) print"Dataframe...\n",dataFrame # filtering on the basis of columns # fetching student with total marks above 400 dataFrame = dataFrame.loc[:, dataFrame.sum(axis=0) > 400] # dataframe print"Updated Dataframe...\n",dataFrame
Output
This will produce the following output −
Dataframe... Jacob_Marks Jamie_Marks Ted_Marks 0 95 77 60 1 90 76 50 2 75 65 65 3 85 45 85 4 88 50 70 Updated Dataframe... Jacob_Marks 0 95 1 90 2 75 3 85 4 88
- Related Articles
- Python Pandas - Filtering few rows from a DataFrame on the basis of sum
- Python - Select multiple columns from a Pandas dataframe
- Python Pandas – How to select DataFrame rows on the basis of conditions
- Python Pandas - Query the columns of a DataFrame
- Python - Renaming the columns of Pandas DataFrame
- Python - Grouping columns in Pandas Dataframe
- Python - Name columns explicitly in a Pandas DataFrame
- Python Pandas – Count the rows and columns in a DataFrame
- Python - Sum only specific rows of a Pandas Dataframe
- Python Pandas - Plot multiple data columns in a DataFrame?
- Python Pandas – Get the datatype and DataFrame columns information
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns
- Plot multiple columns of Pandas dataframe on the bar chart in Matplotlib
- Python – Strip whitespace from a Pandas DataFrame
- Python – Group and calculate the sum of column values of a Pandas DataFrame

Advertisements