Preparing for the Dive into Pandas

Are you ready to delve deeper into the core functionalities of Pandas, one of the most popular Python libraries in data science? Today, we will focus on learning about Pandas DataFrame and data manipulation — the backbone of many data operation tasks. We're going to handle the Titanic dataset — a fascinating example containing real-world data — that will keep you engaged as we navigate through the lesson.

Pandas DataFrames bring versatility and power to the table when it comes to data manipulation. Think of it as Excel, but on steroids, capable of handling large and complex datasets. The processing abilities of DataFrames are crucial for cleaning, transforming, and analyzing datasets in earnest.

Say you've got a dataset, like the Titanic, but some data is missing. Or perhaps there are some anomalies you’d want to filter out. Or you need specific segments of data to examine a particular hypothesis. How would you do it? By mastering Pandas DataFrames, you'd be well-equipped to tackle these tasks!

Initiation to Pandas DataFrame

Pandas DataFrame is a two-dimensional labeled data structure capable of holding data of various types—integers, floats, strings, Python objects, and more. It's generally the most commonly used Pandas object.

Let's start simply by creating a DataFrame from a dictionary:

import pandas as pd

data_dict = {"Name": ["John", "Anna", "Peter"],
             "Age": [28, 24, 33],
             "City": ["New York", "Los Angeles", "Berlin"]}

df = pd.DataFrame(data_dict)

print(df)

"""
    Name  Age         City
0   John   28     New York
1   Anna   24  Los Angeles
2  Peter   33       Berlin
"""

Each key-value pair in the dictionary corresponds to a column in the resulting DataFrame. The key defines the column label, and the corresponding value is a list of column values. The DataFrame constructor takes a dictionary as input and turns it into a two-dimensional table where keys become column names, and values in each key (which should be a list) will be the values for the respective column. Here "John", "Anna", and "Peter" have ages 28, 24, and 33, respectively, and they live in "New York", "Los Angeles", and "Berlin".

DataFrame Characteristics

To inspect the structure and properties of a DataFrame, we have a range of functions at our disposal. Here are some commonly used ones:

  • df.head(n): Returns the first n rows of the DataFrame df.
  • df.tail(n): Returns the last n rows of the DataFrame df.
  • df.shape: Returns a tuple representing the dimensions (number_of_rows, number_of_columns) of the DataFrame df.
  • df.columns: Returns an index containing column labels of the DataFrame df.
  • df.dtypes: Returns a series with the data type of each column.

Let's see these functions in action below:

print(df.head(2))  # Print first two rows
print(df.tail(2))  # Print last two rows
print(df.shape)    # Print dimensions of the df (rows, columns): (3, 3)
print(df.columns)  # Print column labels: Index(['Name', 'Age', 'City'], dtype='object')
print(df.dtypes)   # Print data types of each column:
# Name    object
# Age      int64
# City    object
# dtype: object

These commands will help us understand the basic shape and type of the DataFrame. df.head(2) prints the first 2 rows of the DataFrame, which are {[John, 28, New York], [Anna, 24, Los Angeles]}. df.tail(2) prints the last 2 rows which are {[Anna, 24, Los Angeles}, [Peter, 33, Berlin]}. df.shape gives us the dimensions of the DataFrame here being (3,3) indicating 3 rows and 3 columns. df.columns prints the column names [Name, Age, City] and df.dtypes gives us the data types in each column.

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