Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Python | Pandas dataframe.clip()
Next article icon

Python | Pandas dataframe.clip()

Last Updated : 16 Nov, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.clip() is used to trim values at specified input threshold. We can use this function to put a lower limit and upper limit on the values that any cell can have in the dataframe.
Syntax: DataFrame.clip(lower=None, upper=None, axis=None, inplace=False, *args, **kwargs) Parameters: lower : Minimum threshold value. All values below this threshold will be set to it. upper : Maximum threshold value. All values above this threshold will be set to it. axis : Align object with lower and upper along the given axis. inplace : Whether to perform the operation in place on the data. *args, **kwargs : Additional keywords have no effect but might be accepted for compatibility with numpy.
Example #1: Use clip() function to trim values of a data frame below and above a given threshold value. Python3 1==
# importing pandas as pd import pandas as pd  # Creating a dataframe using dictionary df = pd.DataFrame({"A":[-5, 8, 12, -9, 5, 3],                    "B":[-1, -4, 6, 4, 11, 3],                    "C":[11, 4, -8, 7, 3, -2]})  # Printing the data frame for visualization df 
Now trim all the values below -4 to -4 and all the values above 9 to 9. Values in-between -4 and 9 remaining the same. Python3 1==
# Clip in range (-4, 9) df.clip(-4, 9) 
Output : Notice, there is not any value in the data frame greater than 9 and smaller than -4   Example #2: Use clip() function to clips using specific lower and upper thresholds per column element in the dataframe. Python3 1==
# importing pandas as pd import pandas as pd  # Creating a dataframe using dictionary  df = pd.DataFrame({"A":[-5, 8, 12, -9, 5, 3],                    "B":[-1, -4, 6, 4, 11, 3],                    "C":[11, 4, -8, 7, 3, -2]})  # Printing the dataframe df 
when axis=0, then the value will be clipped across the rows. We are going to provide upper and lower threshold for all the column element (i.e. equivalent to the no. of rows) Creating a Series to store the lower and upper threshold value for each column element. Python3 1==
# lower limit for each individual column element. lower_limit = pd.Series([1, -3, 2, 3, -2, -1])  # upper limit for each individual column element. upper_limit = lower_limit + 5  # Print lower_limit lower_limit  # Print upper_limit upper_limit 
Output : Now we want to apply these limits on the dataframe. Python3 1==
# applying different limit value for each column element df.clip(lower_limit, upper_limit, axis = 0) 
Output :

Next Article
Python | Pandas dataframe.clip()

S

Shubham__Ranjan
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Pandas
  • Python-pandas
  • Python pandas-dataFrame
  • Pandas-DataFrame-Methods
  • AI-ML-DS With Python
Practice Tags :
  • python

Similar Reads

    Pandas Read CSV in Python
    CSV files are the Comma Separated Files. It allows users to load tabular data into a DataFrame, which is a powerful structure for data manipulation and analysis. To access data from the CSV file, we require a function read_csv() from Pandas that retrieves data in the form of the data frame. Here’s a
    6 min read
    Pandas Dataframe/Series.head() method - Python
    The head() method structure and contents of our dataset without printing everything. By default it returns the first five rows but this can be customized to return any number of rows. It is commonly used to verify that data has been loaded correctly, check column names and inspect the initial record
    3 min read
    Pandas Dataframe/Series.tail() method - Python
    The tail() method allows us to quickly preview the last few rows of a DataFrame or Series. This method is useful for data exploration as it helps us to inspect the bottom of the dataset without printing everything. By default it returns the last five rows but this can be customized to return any num
    3 min read
    Pandas Dataframe.sample() | Python
    Pandas DataFrame.sample() function is used to select randomly rows or columns from a DataFrame. It proves particularly helpful while dealing with huge datasets where we want to test or analyze a small representative subset. We can define the number or proportion of items to sample and manage randomn
    2 min read
    Python | Pandas dataframe.info()
    When working with data in Python understanding the structure and content of our dataset is important. The dataframe.info() method in Pandas helps us in providing a concise summary of our DataFrame and it quickly assesses its structure, identify issues like missing values and optimize memory usage.Ke
    2 min read
    Pandas DataFrame dtypes Property | Find DataType of Columns
    Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Pandas DataFrame.dtypes attribute returns a series with the data type of each column.Example:Pythonimport pandas as pd df = pd.DataFrame({'Weight': [45, 88, 56,
    3 min read
    Pandas df.size, df.shape and df.ndim Methods
    Understanding structure of our data is an important step in data analysis and Pandas helps in making this easy with its df.size, df.shape and df.ndim functions. They allow us to identify the size, shape and dimensions of our DataFrame. In this article, we will see how to implement these functions in
    2 min read
    Pandas DataFrame describe() Method
    The describe() method in Pandas generates descriptive statistics of DataFrame columns which provides key metrics like mean, standard deviation, percentiles and more. It works with numeric data by default but can also handle categorical data which offers insights like the most frequent value and the
    4 min read
    Python | Pandas Series.unique()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier. While analyzing the data, many times the user wants to see the unique values in a par
    1 min read
    Pandas dataframe.nunique() Method
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Pandas DataFrame.nunique Syntax Pandas dataframe.nunique() function returns a Series
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences