Back to Python
2025-12-145 min read

Pandas (Python Programming)

Learn Pandas (Python Programming) step by step with clear examples and exercises.

Why This Matters

Pandas is a powerful open-source library in Python that simplifies data manipulation and analysis. It's built on top of NumPy, making it efficient at handling large datasets while offering tools for cleaning, transforming, and analyzing data. Let's dive into the world of Pandas!

Why This Matters

In today's data-driven world, working with data is crucial. Whether you're a data analyst, researcher, or developer, being proficient in handling and analyzing data is essential. That's where Pandas comes in, providing an easy-to-use interface for managing and understanding complex datasets.

Prerequisites

Before diving into Pandas, you should have a good understanding of:

  1. Python programming basics
  2. Intermediate level of NumPy (for understanding some underlying concepts)
  3. Familiarity with basic data structures like lists and dictionaries in Python

Core Concept

Data Structures in Pandas

Pandas revolves around two primary data structures: Series and DataFrame.

  1. Series: A one-dimensional labeled array capable of holding any data type (integers, strings, floating-point numbers, Python objects, etc.). It's similar to a column in a spreadsheet or a database table.
  2. DataFrame: A two-dimensional, size-mutable and potentially heterogeneous tabular data structure with labeled axes (rows and columns). It’s more like an Excel spreadsheet or a SQL table.

Installation and Setup

To use Pandas, first, you need to install it using pip:

pip install pandas

You can also use Jupyter Notebook for interactive coding sessions.

Creating DataFrames

Creating a DataFrame is straightforward:

import pandas as pd

data = {'Name': ['John', 'Anna', 'Peter'],
'Age': [28, 24, 35],
'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)

This creates a DataFrame with three columns (Name, Age, City) and three rows of data.

Accessing DataFrame Indexing and Selecting Data

You can access individual elements or entire columns using the [] operator:

print(df['Name']) # Output: 0 John

1 Anna

2 Peter

Name: 3, dtype: object

print(df.Age[1]) # Output: 24

### Slicing
You can slice a DataFrame to get specific rows or columns:

print(df[1:]) # Output all rows except the first one

print(df['City'][0:2]) # Output the first two cities

### Filtering
Filtering data is also possible using conditional statements:

filtered_data = df[df.Age > 30]

print(filtered_data)

### Merging, Joining and Concatenating DataFrames
You can merge or join multiple DataFrames based on a common column:

Create another DataFrame

another_df = pd.DataFrame({'Name': ['John', 'Anna'], 'Hobby': ['Reading', 'Dancing']})

merged_df = pd.merge(df, another_df, on='Name')

print(merged_df)

### Sorting
Sort a DataFrame by any column:

sorted_df = df.sort_values('Age')

print(sorted_df)

### Pivot Table in Pandas
Creating a pivot table is as simple as calling the `pivot_table()` function:

pivot_table = pd.pivot_table(df, values='Age', index='Name', columns='City', aggfunc='mean')

print(pivot_table)

Worked Example

Let's work with a more complex dataset and perform various operations:

  1. Load data from a CSV file
  2. Clean the data (handle missing values, filtering, removing duplicates)
  3. Perform statistical analysis
  4. Visualize the data using Matplotlib
import pandas as pd
import matplotlib.pyplot as plt

Load data from a CSV file

data = pd.read_csv('sales_data.csv')

Clean the data (handle missing values, filtering, removing duplicates)

cleaned_data = data.dropna() # Drop rows with missing values

filtered_data = cleaned_data[cleaned_data['Sales'] > 1000] # Filter out sales below $1000

unique_data = filtered_data.drop_duplicates() # Remove duplicate rows

Perform statistical analysis

average_sales = unique_data['Sales'].mean()

standard_deviation = unique_data['Sales'].std()

Visualize the data using Matplotlib

plt.hist(unique_data['Sales'], bins=10)

plt.xlabel('Sales')

plt.ylabel('Frequency')

plt.title('Histogram of Sales Data')

plt.show()

Common Mistakes

  1. Not understanding the difference between Series and DataFrame: Although both are used for data manipulation, they have different dimensions (1D vs 2D).
  2. Ignoring missing values: Not handling missing values can lead to incorrect results in statistical analysis.
  3. Merging or joining DataFrames improperly: Make sure the columns being merged or joined have compatible data types and are named identically across DataFrames.
  4. Not sorting DataFrames: Sorting can be crucial for understanding the order of data, especially when working with time-series data.
  5. Overlooking duplicates: Duplicate rows can lead to biased results in statistical analysis.

Practice Questions

  1. Write a script that reads data from an Excel file and calculates the total sales for each city.
  2. Given two DataFrames, write a function that merges them based on a common column and returns the merged DataFrame.
  3. Write a script that filters out sales below a certain threshold (e.g., $500) and visualizes the remaining data using a bar plot.

FAQ

  1. What is the difference between Pandas and NumPy?
  • NumPy is a library for working with arrays and matrices in Python, while Pandas provides higher-level data structures (Series and DataFrame) and functions for manipulating and analyzing data.
  1. How do I handle missing values in my dataset?
  • You can use the dropna() function to remove rows with missing values or the fillna() function to fill them with a specified value.
  1. Can I merge DataFrames based on multiple columns?
  • Yes, you can specify multiple columns when merging DataFrames using the merge() function.
  1. How do I sort a DataFrame by multiple columns?
  • You can use the sort_values() function and pass a list of column names to sort by multiple columns.
  1. What is the best way to learn Pandas effectively?
  • Practice is key! Work with real datasets, experiment with different functions, and don't hesitate to ask for help when you get stuck.
Pandas (Python Programming) | Python | XQA Learn