Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Pandas Data Loading and Inspection

Welcome to your second lesson. In our previous session, we successfully set up your Google Colab environment, our digital workbench for this course. You now have a space where you can execute Python code without any local installation hurdles.

Today, we will put that environment to immediate use to achieve our next learning outcome: loading, inspecting, and summarizing datasets using the pandas library. Pandas is the most important tool in the Python data science ecosystem for data manipulation and analysis. Think of it as the equivalent of a spreadsheet program like Excel, but supercharged with the power of programming. For any analytics or machine learning project, the first step is always to get acquainted with your data. This lesson will equip you with the fundamental commands to do just that, laying the groundwork for the more complex modeling tasks to come.

The Heart of Pandas: DataFrames and Series

Before we load any data, let's understand the two core components of pandas.

  • A DataFrame is a two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labeled axes (rows and columns). It's the primary pandas object you will work with and is conceptually equivalent to a table in a SQL database or a sheet in an Excel file.
  • A Series is a one-dimensional labeled array capable of holding any data type. You can think of a Series as a single column of a DataFrame.

To get a more formal introduction, please read the first part of this excellent tutorial on pandas.

Python Pandas Tutorial: A Complete Introduction for Beginners

This article from learndatasci.com introduces the purpose of pandas and its core components.

Please read the first two main sections: "What's Pandas for?" to understand its role. "Core components of pandas" for a definition of Series and DataFrame.

As you've just read, a DataFrame organizes data into a familiar row-and-column format, which is the standard for nearly all data analysis tasks.

Loading Data into a DataFrame

The most common first step in any data analysis workflow is to load your data from a file into a pandas DataFrame. While pandas can read from many sources (Excel, SQL databases, JSON), the most prevalent format is the Comma-Separated Values (CSV) file.

The command to do this is pd.read_csv(). A great feature of this function is that it can read a file not just from your local computer or Google Drive, but also directly from a URL. This is perfect for getting started quickly in a cloud environment like Colab.

Let's put this into practice. Open the Colab notebook you created in the last lesson. In a new code cell, type and run the following code.




# First, we import the pandas library and give it the conventional alias 'pd'
import pandas as pd




# We provide the URL of a public dataset (the famous Iris dataset)
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"




# We use pd.read_csv() to load the data from the URL into a DataFrame called 'df'
df = pd.read_csv(url)

After running this cell, the df variable now holds the entire Iris dataset. You have successfully loaded your first dataset! This simple process is shown in the first part of the following video.

Exploratory Data Analysis with Pandas Python

This video by Rob Mulla demonstrates the initial steps of an exploratory data analysis project, starting with importing libraries and loading data.

Watch the segment from importing and loading. Note how he first imports pandas as pd and then uses pd.read_csv() to create a DataFrame. The environment he uses (Kaggle) is very similar to Colab.

First Look: Inspecting Your DataFrame

Now that the data is loaded into the df variable, it's time to inspect it. You should never work with data blindly; this inspection phase is crucial for building an initial understanding of its structure and contents.

Here are the five essential commands for your first look at any new dataset. Run each of them in new cells in your Colab notebook.

  1. df.head(): Shows the first 5 rows of the DataFrame. This is the single most common command to get a quick feel for the data and verify it loaded correctly.
  2. df.shape: This is an attribute (not a function, so no parentheses) that returns a tuple representing the dimensions of the DataFrame in the format (rows, columns).
  3. df.columns: This attribute returns the names of all the columns.
  4. df.info(): This method provides a concise technical summary. It shows the number of entries, the column names, the number of non-null values in each column, the data type (Dtype) of each column, and the memory usage. This is extremely useful for spotting potential issues like missing data or incorrect data types.

The following image shows an example of running these commands on a different dataset.

Example output from the `.head()`, `.columns`, and `.info()` commands. These provide a snapshot of the DataFrame's structure, column names, data types, and non-null counts.

To see these commands used in a live analysis, watch the following clip.

Exploratory Data Analysis with Pandas Python

This clip continues the exploratory analysis, demonstrating the fundamental inspection commands.

Watch the segment covering basic data understanding. The presenter walks through using df.shape, df.head(), df.columns, and df.dtypes (which is very similar to df.info()).

High-Level Summary: describe() and value_counts()

After inspecting the structure, the next step is to get a statistical summary of the data.

Numerical Summary with describe()

For columns containing numerical data, the describe() method is a powerhouse. It computes a range of summary statistics for each numerical column in one go:

  • count: The number of non-missing values.
  • mean: The average value.
  • std: The standard deviation, a measure of spread.
  • min, max: The minimum and maximum values.
  • 25%, 50%, 75%: The quartiles, which give you a sense of the data's distribution. The 50th percentile is the median.

Run df.describe() in your notebook. The output provides immediate insights. For instance, you can see the range of sepal lengths or the average petal width in the Iris dataset.

The output of the `.describe()` method provides key descriptive statistics for all numerical columns. This is often a key input for business stakeholders to understand the scale and distribution of key metrics.

Categorical Summary with value_counts()

For columns containing categorical data (like the species column in our Iris dataset), describe() is less useful. Instead, we use the value_counts() method. This function returns a count of each unique value in the Series (column).

To use it, you first select the column you're interested in using square brackets df['column_name'], and then call the method:




# Get the counts for each species in the 'species' column
df['species'].value_counts()

Run this in your notebook. It will tell you exactly how many samples of each iris species are in the dataset. This is vital for understanding class balance, a topic we will revisit in detail when we study classification models.

You can learn more about these summarization techniques by reading the following section.

Python Pandas Tutorial: A Complete Introduction for Beginners

This section of the tutorial covers both numerical and categorical summarization techniques.

Please read the part of the article titled "Understanding your variables". Pay close attention to the explanations of .describe() for both continuous and categorical data, as well as .value_counts().

Conclusion

In this lesson, you've taken a major practical step in your data science journey. You have learned how to use the pandas library to perform the first and most critical phase of any data analysis project: getting to know your data.

Here are the key takeaways:

  • Pandas DataFrames are the primary structure for holding tabular data in Python.
  • You can load data directly from a URL into a DataFrame using pd.read_csv().
  • To inspect the data's structure, you use df.head(), df.shape, and df.info().
  • To get a high-level statistical summary, you use df.describe() for numerical columns and df['column'].value_counts() for categorical columns.

Mastering these commands forms the foundation of data exploration. You are now able to load a standard dataset and generate a first-pass report on its size, shape, and content.

In our next lesson, we will build directly on this foundation. Now that you can load and view data, you'll learn how to perform data manipulation: filtering rows, selecting columns, and creating new columns to start answering specific questions with your data.

Can't find a good explanation? Sign up and we'll make it for you

Sign up