| by Arround The Web | No comments

Pandas Floor

Pandas library in Python supports different methods, modules, and data structures to manipulate and analyze different types of data. To round down the number to a specific level or the nearest integers, the floor technique is used in Python. We can also round down the number of DataFrame columns to the nearest smallest integers by utilizing the “numpy.floor()” method with the “df.apply()”.

This write-up will present a complete overview of Panda’s floor using numerous examples.

How to Determine the Pandas Floor in Python?

To determine the floor value of the Pandas DataFrame and Series object, the “df.apply()” method is used along with the “numpy.floor” function. Let’s consider the following example:

Example 1: Determine the Pandas DataFrame Floor Value Using the “numpy.floor”

Let’s overview the following code to retrieve the floor value of the DataFrame column:

import pandas
import numpy
df = pandas.DataFrame({'Name': ['Anna', 'Joseph', 'Lily','Henry'],'Points': [20.25, 15.52, 41.63, 13.09]})
print(df, '\n')
df['Points'] = df['Points'].apply(numpy.floor)
print(df)

 

In the above-given code block:

  • First, we imported the “numpy” and “pandas” libraries.
  • After that, using the “pandas.DataFrame()” function constructed the DataFrame with several columns.
  • Next, the “df.apply()” method applied the “numpy.floor” function on the “points” columns to convert the round column value to the nearest smallest integers.

Output

The “points” column of the DataFrame has been truncated to the smallest integers.

Example 2: Determine the Pandas Series Floor Value Using the “numpy.floor”

The following code is used to determine the floor value of the Pandas Series:

import pandas
import numpy
series1 = pandas.Series([20.25, 15.52, 41.63, 13.09,-16.8])
print(series1, '\n')
print(numpy.floor(series1))

 

Here:

  • Initially, imported the required modules. Then, used the “pandas.Series()” function that takes the list data and creates the series object.
  • Next, invoked the “numpy.floor()” method that takes the series object as an argument and retrieves the value by rounding down the number to the nearest integers.

Output

The series object values have been rounded down to the nearest integers.

Difference Between the Ceil and Floor Value in Python

The ceiling in Python is used to round up the number to the nearest integers, while the floor in Python is used to round down the number to the nearest integers. In Python, various methods are used to floor or ceil the value, such as math.floor(). numpy.floor(), and others. Let’s utilize one of them to understand the difference between them using the below code:

import math
print('Floor Value: ', math.floor(3.145))
print('\nCeil Value: ',math.ceil(3.145))

 

According to the above-stated code, the “math.floor()” method is used to retrieve the nearest smallest integers while the “math.ceil()” method is used to retrieve the nearest greatest integers value.

As you can see, the floor value “3” and the ceil value “4” have been returned as an output:

Conclusion

The “numpy.floor()” function is used along with the “df.apply()” method to determine the floor value of the Pandas DataFrame and Series object. This method is used to retrieve the value that is nearest to the smallest integers. The comparison between the ceil and floor value can also be demonstrated in this article using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply