Getting First n Rows of DataFrame

Write a Pandas program to get the first 3 rows of a given DataFrame.

Example 1:

Input: DataFrame 
Output: First 3 rows of DataFrame

Example 2:

Input: DataFrame 
Output: First 3 rows of DataFrame

Use pandas DataFrame function head() to get first n rows of the DataFrame.

import pandas as pd

def get_first_rows(df):
    return df.head(3)

print(get_first_rows(create_df({'Name': ['Tom', 'Jack', 'Steve', 'Ricky'],'Age': [28, 34, 29, 42]}, index=['rank1', 'rank2', 'rank3', 'rank4'])))
print(get_first_rows(create_df({'Country': ['USA', 'Canada', 'Germany', 'UK'],'Population': [328, 37, 83, 66]}, index=['rank1', 'rank2', 'rank3', 'rank4'])))

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!