Basic Data Inspection in Pandas

Introduction to Data Inspection

Hello! In this lesson, we will explore the fundamental techniques for inspecting financial data using the Pandas library in Python. Our goal is to enable you to load financial data, inspect its structure, and perform basic data analysis. Let's get started!

Loading and Displaying Data

First, let's recap how to import the necessary libraries and load the dataset. In this scenario, we'll use Tesla (TSLA) historical stock prices.

  1. Import Libraries: We need to import pandas for data manipulation and the datasets library to load our data.
  2. Load the Dataset: We use the load_dataset function from the datasets library to load the Tesla dataset.
  3. Convert to DataFrame: We convert the loaded dataset into a Pandas DataFrame.
  4. Display Data: Using the head() and tail() methods, we can view the first few and last few rows of the dataset, respectively.

Here's the code to achieve this:

import pandas as pd
import datasets

# Load TSLA dataset
tesla_data = datasets.load_dataset('codesignal/tsla-historic-prices')
tesla_df = pd.DataFrame(tesla_data['train'])

# Display first 5 rows of the DataFrame
print(tesla_df.head())

This code snippet loads the TSLA dataset and displays the first 5 rows to help us get a quick look at the data.

Inspecting Data Structure

Next, we want to understand the structure of our dataset. This involves examining the columns, data types, and the number of non-null entries. The info() method of a Pandas DataFrame provides a concise summary of these details.

  • Data Structure Information: The info() method reveals important aspects such as:
    • Column names and data types
    • Non-null counts for each column

Here's the code to inspect the data structure:

# Print basic information about the dataset
print(tesla_df.info())

The output will be:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3347 entries, 0 to 3346
Data columns (total 7 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   Date       3347 non-null   object 
 1   Open       3347 non-null   float64
 2   High       3347 non-null   float64
 3   Low        3347 non-null   float64
 4   Close      3347 non-null   float64
 5   Adj Close  3347 non-null   float64
 6   Volume     3347 non-null   int64  
dtypes: float64(5), int64(1), object(1)
memory usage: 183.2+ KB
None

This output summarizes the dataset structure, showing that it consists of 3347 entries with 7 different columns. It also highlights that there are no missing values in the dataset, and it provides the data type of each column, which is essential to understand before performing any data manipulation or analysis.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal