Skip to main content
Back to problems
#2891
Easy pandas

Method chaining

76.2% acceptance
Mar 2, 2026
109
6
DataFrame animals +-------------+--------+ | Column Name | Type | +-------------+--------+ | name | object | | species | object | | age | int | | weight | int | +-------------+--------+ Write a solution to list the names of animals that weigh strictly more than 100 kilograms. Return the animals sorted by weight in descending order. The result format is in the following example.

Solution

Pandas
Time O(1)
Space O(1)
LeetCode
solution.pandas
# DataFrame animals
# +-------------+--------+
# | Column Name | Type   |
# +-------------+--------+
# | name        | object |
# | species     | object |
# | age         | int    |
# | weight      | int    |
# +-------------+--------+
# Write a solution to list the names of animals that weigh strictly more than 100 kilograms.

# Return the animals sorted by weight in descending order.

# The result format is in the following example.


# Example 1:

# Input:
# DataFrame animals:
# +----------+---------+-----+--------+
# | name     | species | age | weight |
# +----------+---------+-----+--------+
# | Tatiana  | Snake   | 98  | 464    |
# | Khaled   | Giraffe | 50  | 41     |
# | Alex     | Leopard | 6   | 328    |
# | Jonathan | Monkey  | 45  | 463    |
# | Stefan   | Bear    | 100 | 50     |
# | Tommy    | Panda   | 26  | 349    |
# +----------+---------+-----+--------+
# Output:
# +----------+
# | name     |
# +----------+
# | Tatiana  |
# | Jonathan |
# | Tommy    |
# | Alex     |
# +----------+
# Explanation:
# All animals weighing more than 100 should be included in the results table.
# Tatiana's weight is 464, Jonathan's weight is 463, Tommy's weight is 349, and Alex's weight is 328.
# The results should be sorted in descending order of weight.

import pandas as pd


def findHeavyAnimals(animals: pd.DataFrame) -> pd.DataFrame:
  return animals[animals["weight"] > 100].sort_values("weight", ascending=False)[
    ["name"]
  ]