How do i show the first 10 rows in python?

Last update on August 19 2022 21:50:47 [UTC/GMT +8 hours]

Pandas: IMDb Movies Exercise-9 with Solution

Write a Pandas program to display the first 10 rows of the DataFrame.

Sample Solution:

Python Code :

import pandas as pd
df = pd.read_csv['movies_metadata.csv']
#Display the first 10 rows
result = df.head[10]
print["First 10 rows of the DataFrame:"]
print[result]

Sample Output:

First 10 rows of the DataFrame:
   adult    ...     vote_count
0  False    ...           5415
1  False    ...           2413
2  False    ...             92
3  False    ...             34
4  False    ...            173
5  False    ...           1886
6  False    ...            141
7  False    ...             45
8  False    ...            174
9  False    ...           1194

[10 rows x 24 columns]                                     

Python-Pandas Code Editor:

Sample Table:

Have another way to solve this solution? Contribute your code [and comments] through Disqus.

Previous: Write a Pandas program to create a smaller DataFrame with a subset of all features.
Next: Write a Pandas program to sort the DataFrame based on release_date.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Python: Tips of the Day

Creating a sequence of numbers [zero to ten with skips]:

>>> range[0,10,2]
[0, 2, 4, 6, 8]  

  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation

You can use df.head[] to get the first N rows in Pandas DataFrame.

For example, if you need the first 4 rows, then use:

df.head[4]

Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows.

For example, you can use the following syntax to get all the rows excluding the last 4 rows:

df.head[-4]

Complete example to get the first N rows in Pandas DataFrame

Step 1: Create a DataFrame

Let’s create a simple DataFrame with 10 rows:

import pandas as pd

data = {'Fruits': ['Banana','Blueberry','Apple','Cherry','Mango','Pineapple','Watermelon','Papaya','Pear','Coconut'],
        'Price': [2,1.5,3,2.5,3,4,5.5,3.5,1.5,2]
        }

df = pd.DataFrame[data, columns = ['Fruits', 'Price']]

print [df]

As you can see, there are 10 rows in the DataFrame:

       Fruits  Price
0      Banana    2.0
1   Blueberry    1.5
2       Apple    3.0
3      Cherry    2.5
4       Mango    3.0
5   Pineapple    4.0
6  Watermelon    5.5
7      Papaya    3.5
8        Pear    1.5
9     Coconut    2.0

Step 2: Get the first N Rows in Pandas DataFrame

You can use the following syntax to get the first 4 rows in the DataFrame [depending on your needs, you may specify a different number of rows inside the brackets]:

df.head[4]

Here is the complete code to get the first 4 rows for our example:

import pandas as pd

data = {'Fruits': ['Banana','Blueberry','Apple','Cherry','Mango','Pineapple','Watermelon','Papaya','Pear','Coconut'],
        'Price': [2,1.5,3,2.5,3,4,5.5,3.5,1.5,2]
        }

df = pd.DataFrame[data, columns = ['Fruits', 'Price']]

get_rows = df.head[4]

print [get_rows]

You’ll now get the first 4 rows:

      Fruits  Price
0     Banana    2.0
1  Blueberry    1.5
2      Apple    3.0
3     Cherry    2.5

Step 3 [Optional]: Get all the rows, excluding the last N rows

Let’s suppose that you’d like to get all the rows, excluding the last N rows.

For example, you can use the code below in order to get all the rows excluding the last 4 rows:

import pandas as pd

data = {'Fruits': ['Banana','Blueberry','Apple','Cherry','Mango','Pineapple','Watermelon','Papaya','Pear','Coconut'],
        'Price': [2,1.5,3,2.5,3,4,5.5,3.5,1.5,2]
        }

df = pd.DataFrame[data, columns = ['Fruits', 'Price']]

get_rows = df.head[-4]

print [get_rows]

You’ll now see all the rows excluding the last 4 rows:

      Fruits  Price
0     Banana    2.0
1  Blueberry    1.5
2      Apple    3.0
3     Cherry    2.5
4      Mango    3.0
5  Pineapple    4.0

You can check the Pandas Documentation to learn more about df.head[].

How do I get the first 10 rows in Python?

You can use df. head[] to get the first N rows in Pandas DataFrame. Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows.

How do I get the first 5 rows in Python?

By default, head[] function returns first 5 rows.

How do I print the top 5 rows in Python?

head[] prints the indexes. dataframe. to_string[index=False,max_rows=10] prints the first 5 and last 5 rows. Omit print[] , use only ndf.

How do you select top 10 rows in a data frame?

pandas.DataFrame.head[] In Python's Pandas module, the Dataframe class provides a head[] function to fetch top rows from a Dataframe i.e. It returns the first n rows from a dataframe. If n is not provided then default value is 5.

Chủ Đề