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.
1. 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.
# Below are some quick examples
# 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)
2. 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
3. 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
4. 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
5. 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 heighest 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
6. 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)
7. 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 !!
Related Articles
- Pandas Stack Two Series Vertically and Horizontally
- How to Change Position of a Column in Pandas
- How to Convert NumPy Array to Pandas Series
- Convert Pandas Series of Lists to One Series
- Convert Pandas Series to NumPy Array
- Convert Pandas DataFrame to Series
- How to replace Pandas Series?
- How to append Pandas Series?
- Check values are Pandas Series unique
- How to rename Pandas Series?
- How to reshape the Pandas Series?
- How to get Values from Pandas Series?