Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Viewing the Data | Analyzing the Data
Pandas First Steps

bookViewing the Data

To view the first few rows of a dataset, we can utilize the head() method. This method accepts an integer as its argument, which specifies the number of rows to display (by default, it shows first 5 rows). Let's take a look at the first 10 rows of our dataset:

df = pd.read_csv('file.csv')
# Extracting the first 10 rows
first_lines = df.head(10)

If we want to see the last few rows of a DataFrame, we can use the tail() method. It works similarly to the head() method:

df = pd.read_csv('file.csv')
# Extracting the last 10 rows
last_lines = df.tail(10)

Another useful method for exploring DataFrames is sample(). This method fetches random records from a DataFrame. By default, it retrieves a single random record unless specified otherwise.

df = pd.read_csv('file.csv')
# Extracting 10 random rows
random_lines = df.sample(10)
Task

Swipe to start coding

You are given a DataFrame named wine_data.

  • Extract the first 10 rows from this DataFrame and store the result in the first_lines variable.
  • Retrieve the last 15 rows from this DataFrame and store the result in the last_lines variable.
  • Select a random sample of 12 rows from this DataFrame and store the result in the random_rows variable.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain what happens if I use a number larger than the number of rows in the dataset with these methods?

What other ways can I explore the contents of a DataFrame?

How can I display specific columns along with these rows?

close

Awesome!

Completion rate improved to 3.03

bookViewing the Data

Swipe to show menu

To view the first few rows of a dataset, we can utilize the head() method. This method accepts an integer as its argument, which specifies the number of rows to display (by default, it shows first 5 rows). Let's take a look at the first 10 rows of our dataset:

df = pd.read_csv('file.csv')
# Extracting the first 10 rows
first_lines = df.head(10)

If we want to see the last few rows of a DataFrame, we can use the tail() method. It works similarly to the head() method:

df = pd.read_csv('file.csv')
# Extracting the last 10 rows
last_lines = df.tail(10)

Another useful method for exploring DataFrames is sample(). This method fetches random records from a DataFrame. By default, it retrieves a single random record unless specified otherwise.

df = pd.read_csv('file.csv')
# Extracting 10 random rows
random_lines = df.sample(10)
Task

Swipe to start coding

You are given a DataFrame named wine_data.

  • Extract the first 10 rows from this DataFrame and store the result in the first_lines variable.
  • Retrieve the last 15 rows from this DataFrame and store the result in the last_lines variable.
  • Select a random sample of 12 rows from this DataFrame and store the result in the random_rows variable.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
single

single

some-alt