To find the floor of numbers in a column using pandas, the easiest way is to use the numpy floor() function.

df["Column"] = df["Column"].apply(np.floor)

Finding the floor of numbers in a column in pandas is easy. We can round down numbers in a column to the nearest integer with the numpy floor() function.

Let’s say we have the following dataframe.

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [160.20, 123.81, 209.45, 150.35, 102.43, 187.52]})

print(df)
# Output: 
    Name  Weight
0    Jim  160.20
1  Sally  123.81
2    Bob  209.45
3    Sue  150.35
4   Jill  102.43
5  Larry  187.52

To get the floor of the column “weight”, we can apply the numpy floor() function in the following way:

df["Floor of Weight"] = df["Weight"].apply(np.floor)

print(df)

# Output:
    Name  Weight  Floor of Weight
0    Jim  160.20            160.0
1  Sally  123.81            123.0
2    Bob  209.45            209.0
3    Sue  150.35            150.0
4   Jill  102.43            102.0
5  Larry  187.52            187.0

If you are looking to find the floor of a number in regular Python, you can use the Math.floor() function.

If you want to round up a column to the nearest integer, instead of rounding down, you can use the numpy ceil() function.

Hopefully this article has been helpful for you to use the numpy floor() function to find the floor of numbers in a column using pandas in python.

Categorized in:

Python,

Last Update: February 26, 2024