What is Pandas?
Pandas is a popular Python library used for data analysis and data manipulation. It provides powerful data structures such as Series and DataFrame.
Pandas is commonly used when working with CSV files, Excel data, databases, business reports, customer information, sales records, financial data, and other structured datasets.
Series
A one-dimensional labeled collection of data.
DataFrame
A two-dimensional table containing rows and columns.
Data Cleaning
Pandas provides tools for handling missing, duplicate, incorrect, and inconsistent data.
Data Analysis
You can filter, sort, group, summarize, and analyze large datasets efficiently.
Install Pandas using pip.
pip install pandas
In Jupyter Notebook:
!pip install pandas
The standard convention is to import Pandas using the alias pd.
import pandas as pd
print(pd.__version__)
pd is the commonly
used alias for Pandas.
A Pandas Series is a one-dimensional labeled data structure.
import pandas as pd
numbers = pd.Series([10, 20, 30, 40, 50])
print(numbers)
Real-Time Example: Product Prices
import pandas as pd
prices = pd.Series(
[499, 799, 1299, 999],
index=["Laptop Bag", "Keyboard", "Headphones", "Mouse"]
)
print(prices)
A DataFrame is a two-dimensional table with rows and columns.
import pandas as pd
data = {
"Name": ["Rahul", "Priya", "Amit"],
"Age": [25, 28, 24],
"City": ["Pune", "Mumbai", "Delhi"]
}
df = pd.DataFrame(data)
print(df)
Pandas provides several methods for quickly inspecting a DataFrame.
import pandas as pd
df = pd.DataFrame({
"Name": ["A", "B", "C", "D", "E"],
"Salary": [30000, 40000, 50000, 60000, 70000]
})
print(df.head(3))
print(df.tail(2))
head() displays the first rows, while
tail() displays the last rows.
import pandas as pd
df = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Age": [25, 28, 24],
"Salary": [45000, 55000, 40000]
})
print("Shape:", df.shape)
print("Columns:", df.columns)
df.info()
Shape: (3, 3)
Columns: Index(['Name', 'Age', 'Salary'], dtype='object')
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
Column Non-Null Count Dtype
0 Name 3 non-null object
1 Age 3 non-null int64
2 Salary 3 non-null int64
dtypes: int64(2), object(1)
memory usage: 204.0+ bytes
You can select a single column using its column name.
import pandas as pd
df = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Salary": [45000, 55000, 40000]
})
print(df["Salary"])
Selecting Multiple Columns
result = df[["Name", "Salary"]]
print(result)
iloc selects rows and columns using integer
positions.
import pandas as pd
df = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Age": [25, 28, 24],
"Salary": [45000, 55000, 40000]
})
print(df.iloc[0])
print(df.iloc[0:2])
Name Rahul
Age 25
Salary 45000
Name: 0, dtype: object
Name Age Salary
0 Rahul 25 45000
1 Priya 28 55000
iloc is useful when you know the numerical
position of the row or column.
loc selects rows using labels and can also
be used with conditions.
import pandas as pd
df = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Age": [25, 28, 24],
"Salary": [45000, 55000, 40000]
})
print(df.loc[0])
print(df.loc[df["Salary"] > 40000])
Name Rahul
Age 25
Salary 45000
Name: 0, dtype: object
Name Age Salary
0 Rahul 25 45000
1 Priya 28 55000
Filtering is one of the most commonly used Pandas operations in real-world data analysis.
import pandas as pd
employees = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit", "Sneha"],
"Salary": [45000, 65000, 38000, 72000]
})
high_salary = employees[
employees["Salary"] > 50000
]
print(high_salary)
Real-Time Example: Products Above ₹1000
import pandas as pd
products = pd.DataFrame({
"Product": ["Mouse", "Keyboard", "Monitor", "Laptop"],
"Price": [800, 1500, 12000, 55000]
})
expensive = products[
products["Price"] > 1000
]
print(expensive)
Use & for AND conditions and
| for OR conditions.
import pandas as pd
employees = pd.DataFrame({
"Name": ["A", "B", "C", "D"],
"Age": [24, 32, 28, 40],
"Salary": [35000, 60000, 55000, 80000]
})
result = employees[
(employees["Age"] > 25) &
(employees["Salary"] > 50000)
]
print(result)
New columns can be created using existing columns.
import pandas as pd
employees = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Salary": [40000, 50000, 60000]
})
employees["Annual Salary"] = employees["Salary"] * 12
print(employees)
import pandas as pd
employees = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Salary": [40000, 50000, 60000]
})
employees["Salary"] = employees["Salary"] * 1.10
print(employees)
This applies a 10% salary increase to every employee.
Use sort_values() to sort a DataFrame.
import pandas as pd
employees = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit", "Sneha"],
"Salary": [45000, 65000, 40000, 72000]
})
sorted_data = employees.sort_values(
by="Salary",
ascending=False
)
print(sorted_data)
Pandas provides convenient functions for analyzing numerical columns.
import pandas as pd
sales = pd.DataFrame({
"Sales": [12000, 15000, 18000, 22000, 25000]
})
print("Total:", sales["Sales"].sum())
print("Average:", sales["Sales"].mean())
print("Minimum:", sales["Sales"].min())
print("Maximum:", sales["Sales"].max())
print("Median:", sales["Sales"].median())
The describe() method provides a statistical
summary of numerical columns.
import pandas as pd
df = pd.DataFrame({
"Age": [22, 25, 28, 31, 35, 40],
"Salary": [30000, 40000, 50000, 60000, 70000, 90000]
})
print(df.describe())
Age Salary
count 6.000000 6.000000
mean 30.166667 56666.666667
std 6.645801 21602.468995
min 22.000000 30000.000000
25% 25.750000 42500.000000
50% 29.500000 55000.000000
75% 34.000000 67500.000000
max 40.000000 90000.000000
It provides values such as count, mean, standard deviation, minimum, quartiles, and maximum.
Real-world datasets often contain missing values.
Pandas provides isnull(),
dropna(), and fillna().
import pandas as pd
df = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Age": [25, None, 30],
"Salary": [45000, 55000, None]
})
print(df.isnull())
Fill Missing Values
df["Age"] = df["Age"].fillna(
df["Age"].mean()
)
df["Salary"] = df["Salary"].fillna(0)
print(df)
import pandas as pd
df = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Salary": [45000, None, 60000]
})
clean_data = df.dropna()
print(clean_data)
dropna() removes
rows containing missing values. In real applications,
decide whether removing the row or filling the value is
more appropriate for your dataset.
Use drop_duplicates() to remove duplicate rows.
import pandas as pd
customers = pd.DataFrame({
"Name": ["Rahul", "Priya", "Rahul", "Amit"],
"City": ["Pune", "Mumbai", "Pune", "Delhi"]
})
clean_customers = customers.drop_duplicates()
print(clean_customers)
import pandas as pd
df = pd.DataFrame({
"emp_name": ["Rahul", "Priya"],
"emp_salary": [45000, 55000]
})
df = df.rename(columns={
"emp_name": "Name",
"emp_salary": "Salary"
})
print(df)
CSV files are commonly used to store tabular business data.
Pandas can read them using read_csv().
import pandas as pd
df = pd.read_csv("employees.csv")
print(df.head())
Real-Time Example: Sales CSV
Date,Product,Sales
2026-01-01,Laptop,55000
2026-01-02,Mouse,1200
2026-01-03,Keyboard,2500
2026-01-04,Monitor,18000
import pandas as pd
sales = pd.read_csv("sales.csv")
print(sales)
Use to_csv() to save a DataFrame as a CSV file.
import pandas as pd
df = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Salary": [45000, 55000, 60000]
})
df.to_csv("employees.csv", index=False)
Pandas can also work with Excel files.
import pandas as pd
df = pd.read_excel("employees.xlsx")
print(df.head())
Depending on your environment, you may need an Excel
engine such as openpyxl.
import pandas as pd
df = pd.DataFrame({
"Product": ["Laptop", "Mouse", "Keyboard"],
"Sales": [55000, 1200, 2500]
})
df.to_excel(
"sales_report.xlsx",
index=False
)
groupby() is one of the most important
Pandas features for business data analysis.
import pandas as pd
sales = pd.DataFrame({
"Department": [
"Electronics",
"Clothing",
"Electronics",
"Clothing",
"Electronics"
],
"Sales": [
50000,
30000,
45000,
25000,
60000
]
})
result = sales.groupby(
"Department"
)["Sales"].sum()
print(result)
import pandas as pd
sales = pd.DataFrame({
"Department": [
"Electronics",
"Electronics",
"Clothing",
"Clothing"
],
"Sales": [50000, 60000, 30000, 25000]
})
summary = sales.groupby(
"Department"
)["Sales"].agg(
["sum", "mean", "max", "min"]
)
print(summary)
sum mean max min
Department
Clothing 55000 27500.0 30000 25000
Electronics 110000 55000.0 60000 50000
This is useful for generating department-wise business reports.
value_counts() counts how frequently each
value occurs.
import pandas as pd
customers = pd.DataFrame({
"City": [
"Pune",
"Mumbai",
"Pune",
"Delhi",
"Pune",
"Mumbai"
]
})
print(customers["City"].value_counts())
The apply() method can apply a function
to values in a Series or DataFrame.
import pandas as pd
employees = pd.DataFrame({
"Name": ["Rahul", "Priya", "Amit"],
"Salary": [35000, 55000, 85000]
})
def category(salary):
if salary >= 70000:
return "High"
elif salary >= 50000:
return "Medium"
else:
return "Low"
employees["Category"] = employees["Salary"].apply(category)
print(employees)
Use pd.concat() to combine DataFrames.
import pandas as pd
north = pd.DataFrame({
"Employee": ["Rahul", "Priya"],
"Region": ["North", "North"]
})
south = pd.DataFrame({
"Employee": ["Amit", "Sneha"],
"Region": ["South", "South"]
})
all_employees = pd.concat(
[north, south],
ignore_index=True
)
print(all_employees)
Employee Region
0 Rahul North
1 Priya North
2 Amit South
3 Sneha South
merge() is useful when information is
stored across multiple tables.
import pandas as pd
customers = pd.DataFrame({
"CustomerID": [1, 2, 3],
"Name": ["Rahul", "Priya", "Amit"]
})
orders = pd.DataFrame({
"CustomerID": [1, 2, 3],
"Amount": [5000, 7500, 3200]
})
result = pd.merge(
customers,
orders,
on="CustomerID"
)
print(result)
This is similar to combining related tables using a common key.
Pandas provides powerful tools for working with dates and time-series data.
import pandas as pd
dates = pd.to_datetime([
"2026-01-01",
"2026-02-15",
"2026-03-20"
])
print(dates)
DatetimeIndex(['2026-01-01', '2026-02-15', '2026-03-20'],
dtype='datetime64[ns]', freq=None)
Real-Time Example: Order Dates
import pandas as pd
orders = pd.DataFrame({
"OrderDate": [
"2026-01-10",
"2026-02-15",
"2026-03-20"
],
"Amount": [5000, 7500, 9000]
})
orders["OrderDate"] = pd.to_datetime(
orders["OrderDate"]
)
orders["Month"] = orders["OrderDate"].dt.month
print(orders)
OrderDate Amount Month
0 2026-01-10 5000 1
1 2026-02-15 7500 2
2 2026-03-20 9000 3
Pandas can be used to analyze employee information stored in a structured table.
import pandas as pd
employees = pd.DataFrame({
"Name": [
"Rahul",
"Priya",
"Amit",
"Sneha",
"Vikas"
],
"Department": [
"IT",
"HR",
"IT",
"Finance",
"IT"
],
"Salary": [
60000,
50000,
75000,
65000,
80000
]
})
print("Average salary:")
print(employees["Salary"].mean())
print("\nIT employees:")
print(
employees[
employees["Department"] == "IT"
]
)
print("\nDepartment salary:")
print(
employees.groupby(
"Department"
)["Salary"].mean()
)
Average salary:
66000.0
IT employees:
Name Department Salary
0 Rahul IT 60000
2 Amit IT 75000
4 Vikas IT 80000
Department salary:
Department
Finance 65000.0
HR 50000.0
IT 71666.666667
Name: Salary, dtype: float64
An e-commerce company can use Pandas to understand product sales and revenue.
import pandas as pd
sales = pd.DataFrame({
"Product": [
"Laptop",
"Mouse",
"Keyboard",
"Laptop",
"Mouse",
"Monitor"
],
"Quantity": [
2, 10, 5, 3, 8, 4
],
"Price": [
55000,
800,
1500,
55000,
800,
12000
]
})
sales["Revenue"] = (
sales["Quantity"] *
sales["Price"]
)
print(sales)
print("\nTotal Revenue:")
print(sales["Revenue"].sum())
print("\nRevenue by Product:")
print(
sales.groupby(
"Product"
)["Revenue"].sum()
)
Product Quantity Price Revenue
0 Laptop 2 55000 110000
1 Mouse 10 800 8000
2 Keyboard 5 1500 7500
3 Laptop 3 55000 165000
4 Mouse 8 800 6400
5 Monitor 4 12000 48000
Total Revenue:
344000
Revenue by Product:
Product
Keyboard 7500
Laptop 275000
Monitor 48000
Mouse 14400
Name: Revenue, dtype: int64
import pandas as pd
customers = pd.DataFrame({
"Name": [
"Rahul",
"Priya",
"Amit",
"Sneha",
"Vikas"
],
"City": [
"Pune",
"Mumbai",
"Pune",
"Delhi",
"Mumbai"
],
"Orders": [
5, 8, 3, 10, 6
],
"TotalSpent": [
15000,
28000,
9000,
35000,
22000
]
})
print("Average spending:")
print(customers["TotalSpent"].mean())
print("\nTop customers:")
print(
customers.sort_values(
"TotalSpent",
ascending=False
).head(3)
)
print("\nCustomers by city:")
print(
customers["City"].value_counts()
)
Average spending:
21800.0
Top customers:
Name City Orders TotalSpent
3 Sneha Delhi 10 35000
1 Priya Mumbai 8 28000
4 Vikas Mumbai 6 22000
Customers by city:
City
Mumbai 2
Pune 2
Delhi 1
Name: count, dtype: int64
Pandas can identify products that require restocking.
import pandas as pd
inventory = pd.DataFrame({
"Product": [
"Laptop",
"Mouse",
"Keyboard",
"Monitor",
"Printer"
],
"Stock": [
25,
5,
8,
3,
20
],
"ReorderLevel": [
10,
10,
10,
5,
10
]
})
inventory["Needs Restock"] = (
inventory["Stock"] <
inventory["ReorderLevel"]
)
print(inventory)
print("\nProducts requiring restock:")
print(
inventory[
inventory["Needs Restock"]
]
)
Product Stock ReorderLevel Needs Restock
0 Laptop 25 10 False
1 Mouse 5 10 True
2 Keyboard 8 10 True
3 Monitor 3 5 True
4 Printer 20 10 False
Products requiring restock:
Product Stock ReorderLevel Needs Restock
1 Mouse 5 10 True
2 Keyboard 8 10 True
3 Monitor 3 5 True
import pandas as pd
students = pd.DataFrame({
"Name": [
"Rahul",
"Priya",
"Amit",
"Sneha"
],
"Math": [85, 92, 68, 78],
"Science": [80, 95, 70, 82],
"English": [88, 90, 65, 85]
})
students["Average"] = students[
["Math", "Science", "English"]
].mean(axis=1)
students["Result"] = students["Average"].apply(
lambda x: "Pass" if x >= 40 else "Fail"
)
print(students)
print("\nTop students:")
print(
students.sort_values(
"Average",
ascending=False
)
)
Name Math Science English Average Result
0 Rahul 85 80 88 84.333333 Pass
1 Priya 92 95 90 92.333333 Pass
2 Amit 68 70 65 67.666667 Pass
3 Sneha 78 82 85 81.666667 Pass
Top students:
Name Math Science English Average Result
1 Priya 92 95 90 92.333333 Pass
0 Rahul 85 80 88 84.333333 Pass
3 Sneha 78 82 85 81.666667 Pass
2 Amit 68 70 65 67.666667 Pass
This example demonstrates a common reporting workflow: load data, calculate revenue, group data, and generate a summary.
import pandas as pd
sales = pd.DataFrame({
"Month": [
"January",
"January",
"February",
"February",
"March",
"March"
],
"Product": [
"Laptop",
"Mouse",
"Laptop",
"Keyboard",
"Laptop",
"Monitor"
],
"Quantity": [
2, 10, 3, 8, 4, 5
],
"Price": [
55000, 800,
55000, 1500,
55000, 12000
]
})
sales["Revenue"] = (
sales["Quantity"] *
sales["Price"]
)
monthly_report = sales.groupby(
"Month"
)["Revenue"].sum()
print("Monthly Revenue:")
print(monthly_report)
print("\nTotal Revenue:")
print(sales["Revenue"].sum())
Monthly Revenue:
Month
February 177000
January 118000
March 280000
Name: Revenue, dtype: int64
Total Revenue:
575000
| Function / Method | Purpose |
|---|---|
pd.Series() |
Create a Series |
pd.DataFrame() |
Create a DataFrame |
pd.read_csv() |
Read CSV data |
pd.read_excel() |
Read Excel data |
df.head() |
View first rows |
df.tail() |
View last rows |
df.info() |
View DataFrame information |
df.describe() |
Generate statistical summary |
df.sort_values() |
Sort data |
df.groupby() |
Group data |
df.dropna() |
Remove missing rows |
df.fillna() |
Fill missing values |
df.drop_duplicates() |
Remove duplicate records |
df.merge() |
Combine related DataFrames |
pd.concat() |
Concatenate DataFrames |
df.to_csv() |
Export to CSV |
df.to_excel() |
Export to Excel |
| Feature | Series | DataFrame |
|---|---|---|
| Dimensions | 1D | 2D |
| Looks like | Single column | Table |
| Example | pd.Series() |
pd.DataFrame() |
| Common use | Single column of data | Complete dataset |
This example combines several important Pandas concepts into one realistic business workflow.
import pandas as pd
sales = pd.DataFrame({
"Date": [
"2026-01-05",
"2026-01-06",
"2026-01-07",
"2026-01-08",
"2026-01-09",
"2026-01-10"
],
"Product": [
"Laptop",
"Mouse",
"Laptop",
"Keyboard",
"Monitor",
"Mouse"
],
"Category": [
"Electronics",
"Accessories",
"Electronics",
"Accessories",
"Electronics",
"Accessories"
],
"Quantity": [
2, 10, 3, 8, 4, 12
],
"Price": [
55000,
800,
55000,
1500,
12000,
800
]
})
Convert date column
sales["Date"] = pd.to_datetime(
sales["Date"]
)
Calculate revenue
sales["Revenue"] = (
sales["Quantity"] *
sales["Price"]
)
print("Complete Sales Data:")
print(sales)
Total revenue
total_revenue = sales["Revenue"].sum()
print("\nTotal Revenue:")
print(total_revenue)
Product-wise revenue
product_sales = sales.groupby(
"Product"
)["Revenue"].sum()
print("\nProduct-wise Revenue:")
print(product_sales)
Category-wise revenue
category_sales = sales.groupby(
"Category"
)["Revenue"].sum()
print("\nCategory-wise Revenue:")
print(category_sales)
Best-selling transactions
top_sales = sales.sort_values(
"Revenue",
ascending=False
).head(3)
print("\nTop 3 Transactions:")
print(top_sales)
Complete Sales Data:
Date Product Category Quantity Price Revenue
0 2026-01-05 Laptop Electronics 2 55000 110000
1 2026-01-06 Mouse Accessories 10 800 8000
2 2026-01-07 Laptop Electronics 3 55000 165000
3 2026-01-08 Keyboard Accessories 8 1500 12000
4 2026-01-09 Monitor Electronics 4 12000 48000
5 2026-01-10 Mouse Accessories 12 800 9600
Total Revenue:
352600
Product-wise Revenue:
Product
Keyboard 12000
Laptop 275000
Monitor 48000
Mouse 17600
Name: Revenue, dtype: int64
Category-wise Revenue:
Category
Accessories 29600
Electronics 323000
Name: Revenue, dtype: int64
Top 3 Transactions:
Date Product Category Quantity Price Revenue
2 2026-01-07 Laptop Electronics 3 55000 165000
0 2026-01-05 Laptop Electronics 2 55000 110000
4 2026-01-09 Monitor Electronics 4 12000 48000
- Use
import pandas as pdas the standard import. - Inspect data using
head(),tail(), andinfo(). - Use
describe()to understand numerical data. - Check missing values before performing analysis.
- Use meaningful column names.
- Use vectorized Pandas operations when possible.
- Use
groupby()for grouped business analysis. - Use
sort_values()to identify top or bottom records. - Use
merge()when combining related datasets. - Use
concat()when stacking compatible DataFrames. - Convert date columns using
pd.to_datetime(). - Be careful when using
dropna()because it removes data. - Save processed datasets using
to_csv()orto_excel().
- Pandas is primarily used for data manipulation and analysis.
Seriesrepresents one-dimensional labeled data.DataFramerepresents two-dimensional tabular data.- Use
locfor label-based selection. - Use
ilocfor position-based selection. - Use boolean conditions to filter rows.
- Use
groupby()to summarize data by categories. - Use
fillna()ordropna()to handle missing values. - Use
drop_duplicates()to remove duplicate records. - Use
merge()to combine related datasets. - Use
pd.concat()to combine compatible DataFrames. - Use
read_csv()andread_excel()to load external data. - Use
to_csv()andto_excel()to export processed data. - Pandas works especially well with NumPy for numerical data processing.