Method Chaining — LeetCode 2891 Python Solution
EasyPandas
- Problem
- #2891
- Reading time
- 2 min
- Source
- leetcode.com
Table schema
SQL
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.Example
SQL
DataFrame animals
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| name | object |
| species | object |
| age | int |
| weight | int |
+-------------+--------+Python solution
Python
import pandas as pd
def findHeavyAnimals(animals: pd.DataFrame) -> pd.DataFrame:
return animals[animals['weight'] > 100].sort_values('weight', ascending=False)[
['name']
]Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 2891. Method Chaining?
- LeetCode 2891. Method Chaining is rated Easy on LeetCode.
- What topics does LeetCode 2891. Method Chaining cover?
- LeetCode 2891. Method Chaining is tagged Pandas on LeetCode.