How to filter in a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to filter in a Pandas DataFrame?

How to filter in a Pandas DataFrame?

This recipe helps you filter in a Pandas DataFrame

0
This data science python source code does the following: 1. Imports necessary libraries. 2. Creates data dictionary and converts it to pandas dataframe. 3. Selects and visualizes columns and row by filtering them out using customized filters.
In [1]:
## How to filter in a Pandas DataFrame
def Kickstarter_Example_87():
    print()
    print(format('How to filter a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create Dataframe
    data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
            'year': [2012, 2012, 2013, 2014, 2014],
            'reports': [4, 24, 31, 2, 3],
            'coverage': [25, 94, 57, 62, 70]}

    df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
    print(); print(df)

    # View Column
    print(); print(df['name'])

    # View Two Columns
    print(); print(df[['name', 'reports']])

    # View First Two Rows
    print(); print(df[:2])

    # View Rows Where Coverage Is Greater Than 50
    print(); print(df[df['coverage'] > 50])

    # View Rows Where Coverage Is Greater Than 50 And Reports Less Than 4
    print(); print(df[(df['coverage']  > 50) & (df['reports'] < 4)])

Kickstarter_Example_87()
*************************How to filter a Pandas DataFrame*************************

             name  year  reports  coverage
Cochice     Jason  2012        4        25
Pima        Molly  2012       24        94
Santa Cruz   Tina  2013       31        57
Maricopa     Jake  2014        2        62
Yuma          Amy  2014        3        70

Cochice       Jason
Pima          Molly
Santa Cruz     Tina
Maricopa       Jake
Yuma            Amy
Name: name, dtype: object

             name  reports
Cochice     Jason        4
Pima        Molly       24
Santa Cruz   Tina       31
Maricopa     Jake        2
Yuma          Amy        3

          name  year  reports  coverage
Cochice  Jason  2012        4        25
Pima     Molly  2012       24        94

             name  year  reports  coverage
Pima        Molly  2012       24        94
Santa Cruz   Tina  2013       31        57
Maricopa     Jake  2014        2        62
Yuma          Amy  2014        3        70

          name  year  reports  coverage
Maricopa  Jake  2014        2        62
Yuma       Amy  2014        3        70

Relevant Projects

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.