
Sponsored
Sponsored
Most data manipulation libraries provide a direct method to access the top few rows of a DataFrame. This approach utilizes the built-in method to fetch the first three rows efficiently.
Time Complexity: O(1), since it directly accesses the specified rows.
Space Complexity: O(1), no additional space usage beyond the output.
1function getFirstThreeRows(employees) {
2 returnIn JavaScript, if you have a 2D array representing the DataFrame, the slice method helps to extract the first three elements efficiently.
This approach involves manually iterating over the DataFrame and extracting the first three rows. It's useful in languages or environments where built-in functionality might not exist.
Time Complexity: O(1), since we only loop through a fixed number of rows.
Space Complexity: O(1), as we only store a constant number of rows.
1import java.util.List;
2import java.util.ArrayList;
3
4public List<List<String>> getFirstThreeRows(List<List<String>> employees) {
5 return employees.subList(0, Math.min(3, employees.size()));
6}In Java, this approach uses the subList method to extract the first three rows. We take care to not exceed the bounds of the list with Math.min.