Data Science / Data Wrangling and Manipulation

Data Manipulation Techniques with Pandas

Learn how to use the Pandas library for effective data manipulation. This tutorial will guide you through the process of manipulating data using various functions and methods.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explores techniques for data manipulation and wrangling using popular libraries.

Data Manipulation Techniques with Pandas

1. Introduction

This tutorial aims to guide you through the process of data manipulation using the Pandas library in Python. By the end of this tutorial, you will be able to load, inspect, filter, and modify datasets using a variety of Pandas functions and methods.

What Will You Learn?
- Loading data into Pandas
- Inspecting data
- Filtering data
- Modifying data

Prerequisites
This tutorial assumes that you have a basic understanding of Python programming. It would also be helpful to have some familiarity with data structures, particularly lists and dictionaries.

2. Step-by-Step Guide

Loading Data into Pandas

Pandas can read data from a variety of file formats such as CSV, Excel, and SQL databases. The most common function used to load data is pd.read_csv().

import pandas as pd

# Load data from a CSV file
df = pd.read_csv('file.csv')

Inspecting Data

You can inspect the first few rows of the DataFrame using the .head() method, or the last few rows using the .tail() method.

# Print the first 5 rows
print(df.head())

# Print the last 5 rows
print(df.tail())

Filtering Data

Filtering is one of the most frequent data manipulation operation. Pandas provides a powerful and flexible way to filter data.

# Filter rows where column 'A' is greater than 50
filtered_df = df[df['A'] > 50]

Modifying Data

DataFrames are mutable, so you can easily modify them by adding columns, changing values, etc.

# Add a new column 'C' which is the sum of 'A' and 'B'
df['C'] = df['A'] + df['B']

3. Code Examples

Example 1: Load and Inspect Data

import pandas as pd

# Load data
df = pd.read_csv('file.csv')

# Inspect first 5 rows
print(df.head())

Example 2: Filter Data

# Filter rows where 'Age' is greater than 30
filtered_df = df[df['Age'] > 30]

# Print the filtered data
print(filtered_df)

Example 3: Modify Data

# Add a new column 'FullName' which is the combination of 'FirstName' and 'LastName'
df['FullName'] = df['FirstName'] + ' ' + df['LastName']

# Print the DataFrame to check the result
print(df)

4. Summary

In this tutorial, we have covered how to load, inspect, filter, and modify data using Pandas in Python. As next steps, you might want to explore more advanced data manipulation techniques such as grouping and aggregation, joining and merging data, and working with time series data.

5. Practice Exercises

Exercise 1: Load a dataset from a CSV file and print the first 10 rows.

Solution:

# Load data
df = pd.read_csv('file.csv')

# Print the first 10 rows
print(df.head(10))

Exercise 2: Filter the DataFrame to include only rows where 'Age' is less than 20 and 'Gender' is 'Female'. Print the filtered DataFrame.

Solution:

# Filter data
filtered_df = df[(df['Age'] < 20) & (df['Gender'] == 'Female')]

# Print the filtered data
print(filtered_df)

Exercise 3: Add a new column 'AgeInMonths', which is 'Age' multiplied by 12. Print the DataFrame to check the result.

Solution:

# Add a new column 'AgeInMonths'
df['AgeInMonths'] = df['Age'] * 12

# Print the DataFrame
print(df)

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help