Built with Tutify.io

    AIStudyLabs

    Pandas Get Floor or Ceil of Series

    Pandas Get Floor or Ceil of Series

    We can get the floor or ceil (Ceiling) values from the pandas Series by using series.clip(), NumPy's floor() and ceil() functions. In simple words, the floor value is always less than or equal to the given value, and the ceiling value is always greater than or equal to the given value.

    In this article, I will explain how we can get the floor or ceiling of a pandas series in python with several examples.

    Key Points –

    <a id="h-quick-examples-of-floor-or-ceil-of-a-series"></a>

    Quick Examples of Floor or Ceil of a Series

    If you are in a hurry, below are some quick examples of how to floor or ceiling of series in python.

    
    # Quick examples of floor or ceil of a series
    
    # Example 1: Get the floor values of pandas series
    ser2 = np.floor(ser)
    
    # Example 2: Get the ceil values of pandas series
    ser2 = np.ceil(ser)
    
    # Example 3: Use pandas.series.clip() function 
    # To get lower values
    ser2 = ser.clip(lower=0)
    
    # Example 4: Use pandas.series.clip() function 
    # To get upper values
    ser2 = ser.clip(upper=0)
    

    <a id="h-initialize-pandas-series"></a>

    Initialize Pandas Series

    Pandas Series is a one-dimensional, Index-labeled data structure available only in the Pandas library. It can store all the datatypes such as strings, integers, float, and other Python objects. We can access each element in the Series with the help of corresponding default indices.

    Now, let’s create pandas series using a list of values.

    
    import pandas as pd
    import numpy as np
    
    # Create the Series
    ser = pd.Series([2.3,3.5,-1.3,5.7,4.8,-6.8])
    print(ser)
    

    Yields below output.

    
    # Output:
    0    2.3
    1    3.5
    2   -1.3
    3    5.7
    4    4.8
    5   -6.8
    dtype: float64
    

    <a id="h-get-the-floor-values-of-the-pandas-series"></a>

    Get the Floor Values of the Pandas Series

    Using numpy.floor() function we can get the floor of each value in the Series. floor() function takes Series as a parameter and returns the floor value of each Series element with a float data type. The floor value of the scalar x is the largest integer y, such that y<=x.

    
    # Get the floor values of pandas series
    value = np.floor(ser)
    print(value)
    

    Yields below output.

    
    # Output:
    0    2.0
    1    3.0
    2   -2.0
    3    5.0
    4    4.0
    5   -7.0
    dtype: float64
    

    <a id="h-get-the-ceil-values-of-pandas-series"></a>

    Get the Ceil Values of Pandas Series

    Using numpy.ceil() function we can get the ceiling of each value in the Series. The ceil of the scalar x is the smallest integer i, such that i>= x. In simple words, the ceil value is always greater than equal to the given value.

    
    # Get the ceil values of pandas series
    ser2 = np.ceil(ser)
    print(ser2)
    

    Yields below output.

    
    # Output:
    0    3.0
    1    4.0
    2   -1.0
    3    6.0
    4    5.0
    5   -6.0
    dtype: float64
    

    <a id="h-use-pandas-series-clip-function-to-get-lower-amp-upper-values"></a>

    Use pandas.Series.clip() Function to Get Lower & Upper Values

    Pandas Series.clip() is used to get the lower & upper values of the series. If you pass lower=0 into clip() function, it will override the least values with zero's. If you pass upper=0 into clip() function, It will override the highest values with zeros.

    
    # Use pandas.series.clip() function 
    # To get lower values
    ser2 = ser.clip(lower=0)
    print(ser2)
    
    # Output:
    # 0    2.3
    # 1    3.5
    # 2    0.0
    # 3    5.7
    # 4    4.8
    # 5    0.0
    # dtype: float64
    
    # Use pandas.series.clip() function 
    # To get upper values
    ser2 = ser.clip(upper=0)
    print(ser2)
    
    # Output:
    # 0    0.0
    # 1    0.0
    # 2   -1.3
    # 3    0.0
    # 4    0.0
    # 5   -6.8
    # dtype: float64
    

    <a id="h-complete-example-for-floor-or-ceiling-of-a-series"></a>

    Complete Example For Floor or Ceiling of a Series

    
    import pandas as pd
    import numpy as np
    
    # Create the Series
    ser = pd.Series([2.3,3.5,-1.3,5.7,4.8,-6.8])
    print(ser)
    
    # Get the floor values of pandas series
    ser2 = np.floor(ser)
    print(ser2)
    
    # Get the ceil values of pandas series
    ser2 = np.ceil(ser)
    print(ser2)
    
    # Use pandas.series.clip() function 
    # To get lower values
    ser2 = ser.clip(lower=0)
    print(ser2)
    
    # Use pandas.series.clip() function 
    # To get upper values
    ser2 = ser.clip(upper=0)
    print(ser2)
    

    <a id="h-faq-on-pandas-get-floor-or-ceil-of-series"></a>

    FAQ on Pandas Get Floor or Ceil of Series

    To get the floor of each element in a Pandas Series, you can use the numpy library's np.floor() function

    To get the ceiling of a Pandas Series, you can use the numpy library's np.ceil() function.

    To use floor or ceiling functions with a Pandas Series, you need to import the numpy library as it provides np.floor() and np.ceil() functions.

    The np.floor() and np.ceil() functions return a new array or Series. They don't modify the original Series in place.

    Both np.floor() and np.ceil() handle NaN values gracefully and will keep them as NaN in the output.

    Pandas doesn't have built-in .floor() or .ceil() methods directly in the Series or DataFrame. However, numpy functions like np.floor() and np.ceil() are the recommended way to achieve this.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to floor or ceil (ceiling) of a pandas series in python using series.clip(), numpy.floor(), and numpy.ceil() functions with examples.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References