0
0
Pandasdata~15 mins

head() and tail() for previewing in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Previewing Data with head() and tail() in pandas
📖 Scenario: You have a small dataset of daily temperatures recorded in a city for one week. You want to quickly look at the first few and last few days to understand the data.
🎯 Goal: Learn how to use head() and tail() methods in pandas to preview the first and last rows of a dataset.
📋 What You'll Learn
Create a pandas DataFrame with exact temperature data
Use a variable to set the number of rows to preview
Use head() to get the first rows
Use tail() to get the last rows
Print the previewed data
💡 Why This Matters
🌍 Real World
Previewing data quickly helps data scientists understand the structure and spot any obvious issues before deep analysis.
💼 Career
Knowing how to use head() and tail() is a basic skill for data analysts and scientists to inspect datasets efficiently.
Progress0 / 4 steps
1
Create the temperature DataFrame
Create a pandas DataFrame called temps with these exact data: dates as strings in 'Date' column: '2024-06-01', '2024-06-02', '2024-06-03', '2024-06-04', '2024-06-05', '2024-06-06', '2024-06-07'. Temperatures in Celsius in 'Temp' column: 22, 24, 19, 23, 25, 20, 21.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary containing 'Date' and 'Temp' keys and lists of values.

2
Set the number of rows to preview
Create a variable called preview_rows and set it to 3. This will control how many rows to preview from the start and end of the DataFrame.
Pandas
Need a hint?

Just create a variable preview_rows and assign the number 3.

3
Use head() and tail() to preview data
Create two variables: first_rows to store the first preview_rows rows of temps using head(), and last_rows to store the last preview_rows rows of temps using tail().
Pandas
Need a hint?

Use temps.head(preview_rows) and temps.tail(preview_rows) to get the first and last rows.

4
Print the previewed rows
Print the variables first_rows and last_rows to display the first and last 3 rows of the temperature data.
Pandas
Need a hint?

Use print(first_rows) and print(last_rows) to show the previews.