Pandas Tutorial
Pandas is a Python library for working with data in tables, with rows and columns, just like an Excel sheet.
With Pandas you can load a file of thousands of rows, clean it, filter it, and get answers such as "which product sold the most last month?" in a few lines of code.
Why Learn Pandas?
- It is the most used tool for data analysis in Python
- It reads and writes CSV, Excel, JSON and SQL data
- It makes cleaning messy data (missing values, duplicates, wrong types) much easier
- Data analysts, data scientists and AI engineers use it every day
A First Example
Example
import pandas as pd
sales = pd.DataFrame({
"product": ["Tea", "Sugar", "Rice"],
"price": [1200, 180, 350],
"sold": [40, 95, 60]
})
sales["revenue"] = sales["price"] * sales["sold"]
print(sales)product price sold revenue 0 Tea 1200 40 48000 1 Sugar 180 95 17100 2 Rice 350 60 21000
In one line we added a new column that multiplies price by quantity for every row.
What You Will Learn
- Series and DataFrames, the two main Pandas objects
- Reading CSV and other files, and getting a first look at your data
- Selecting rows and columns with loc and iloc, and filtering
- Cleaning data: missing values, duplicates and wrong types
- Sorting, adding columns, grouping and summarising
- Joining tables together
You should know basic Python first, and it helps to know NumPy. New to Python? Start with the Python tutorial.
Exercise
What is a DataFrame in Pandas?
What to Learn After Pandas
Continue with SciPy for statistics and scientific tools, or try R, another language made for data analysis.
Want to learn with a teacher? The Workownix Python Programming course includes data analysis with Pandas and real projects, online or at our institute in Orangi Town, Karachi.
