Get the first 3 rows of a given DataFrame

Get the first 3 rows of a given DataFrame

To get the first 3 rows of a given DataFrame in Python using pandas, you can use the head() method. By default, head() returns the first 5 rows, but you can specify a different number as an argument to retrieve a specific number of rows.

Here's how you can get the first 3 rows of a DataFrame:

import pandas as pd # Sample DataFrame data = { 'A': [1, 2, 3, 4, 5], 'B': [5, 6, 7, 8, 9], 'C': [9, 8, 7, 6, 5] } df = pd.DataFrame(data) # Get the first 3 rows first_three_rows = df.head(3) print(first_three_rows) 

Output:

 A B C 0 1 5 9 1 2 6 8 2 3 7 7 

As shown in the example above, the head(3) method retrieves the first 3 rows of the DataFrame.


More Tags

ipc video function-pointers activeadmin cfeclipse virtual-machine webview visual-studio-code grep regex-greedy

More Programming Guides

Other Guides

More Programming Examples