nsmallest and nlargest

The nsmallest and nlargest functions are extremely useful for returning the n smallest and n largest rows after ordering by the desired column.

In the sample sales data, let's find the 3 smallest records by Profit:

sales_data.nsmallest(3, "Profit")

The following will be the output:

Figure6.73: Result of nsmallest

The ordering can also be done with respect to multiple columns, as shown here:

sales_data.nlargest(3, ["Quantity", "Profit"])

The following will be the output:

Result of nlargest

These functions – nsmallest and nlargest have the keep parameter to decide how duplicates are handled. It helps to choose either the first occurrence, the last occurrence, or to retain all duplicates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset