How can I get the first or last n rows from a table?

Viewed 43

I need a function which takes in an integer *n as input, and checks through a database table to return all the rows from the first to the nth row. The function should also be able to return the last n rows from the table. In the table below, I have 10 rows on the table. If I want to get just the 'head' of the table, or the first 5 rows, I just have to pass 5 to the function and it should return the output I want. This is similar to the df.head() method used in Pandas. The function should not take values equal to or higher than the total number of rows for the table. I am using GridDB python client and I have created my Factory instance and stored my database table.

'''
-- | id | first_name | last_name  | salary_$  |
-- |----|------------|------------|-----------|
-- | 1  | Josh       | Mac        | 175000    |
-- | 2  | Hong       | Williams   | 150000    |
-- | 3  | Ajay       | Kumar      | 125000    |
-- | 4  | Jay        | Mac        | 100000    |
-- | 5  | John       | Jonah      | 95000     |
-- | 6  | Lucy       | Judas      | 80000     |
-- | 7  | Matthew    | Stones     | 80000     |
-- | 8  | Kang       | Choi       | 70000     |
-- | 9  | Rajiv      | Kumar      | 50000     |
-- | 10 | Smith      | Peters     | 50000     |
'''
1 Answers

That's piece of cake

Pandas do have function for viewing last rows

  1. To view last rows (by default viewing lst 5 rows)

    df.tail()

  2. To views last n number of rows

    df.tail(7)

cheers, athrv

Related