Python Pandas

Learn Pandas DataFrames, Series, data cleaning, filtering, grouping, sorting, CSV files, missing values, and real-time data-analysis examples in Python.

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.

1. Installing Pandas

Install Pandas using pip.

Terminal
pip install pandas

In Jupyter Notebook:

Jupyter Notebook
!pip install pandas
2. Importing Pandas

The standard convention is to import Pandas using the alias pd.

Python
import pandas as pd

print(pd.__version__)
Note: pd is the commonly used alias for Pandas.
3. Creating a Pandas Series

A Pandas Series is a one-dimensional labeled data structure.

Series Example
import pandas as pd

numbers = pd.Series([10, 20, 30, 40, 50])

print(numbers)
0 10 1 20 2 30 3 40 4 50 dtype: int64

Real-Time Example: Product Prices

Product Prices
import pandas as pd

prices = pd.Series(
    [499, 799, 1299, 999],
    index=["Laptop Bag", "Keyboard", "Headphones", "Mouse"]
)

print(prices)
Laptop Bag 499 Keyboard 799 Headphones 1299 Mouse 999 dtype: int64
4. Creating a DataFrame

A DataFrame is a two-dimensional table with rows and columns.

Raw Data
→
DataFrame
→
Analysis
→
Business Result
DataFrame
import pandas as pd

data = {
    "Name": ["Rahul", "Priya", "Amit"],
    "Age": [25, 28, 24],
    "City": ["Pune", "Mumbai", "Delhi"]
}

df = pd.DataFrame(data)

print(df)
Name Age City 0 Rahul 25 Pune 1 Priya 28 Mumbai 2 Amit 24 Delhi
5. Viewing Data

Pandas provides several methods for quickly inspecting a DataFrame.

head() and tail()
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.

6. DataFrame Shape and Information
shape, columns and info
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()
Output
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
shape gives the number of rows and columns. columns gives the column names. info() provides useful information about data types and non-null values.
7. Selecting a Column

You can select a single column using its column name.

Select Column
import pandas as pd

df = pd.DataFrame({
    "Name": ["Rahul", "Priya", "Amit"],
    "Salary": [45000, 55000, 40000]
})

print(df["Salary"])
0 45000 1 55000 2 40000 Name: Salary, dtype: int64

Selecting Multiple Columns

Multiple Columns
result = df[["Name", "Salary"]]

print(result)
8. Selecting Rows with iloc

iloc selects rows and columns using integer positions.

iloc
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])
▶ Output
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.

9. Selecting Rows with loc

loc selects rows using labels and can also be used with conditions.

loc
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])
▶ Output
Name        Rahul


Age 25
Salary 45000
Name: 0, dtype: object

Name  Age  Salary


0 Rahul 25 45000
1 Priya 28 55000
loc uses row labels, while conditions can be used to filter rows based on column values.
10. Filtering Data

Filtering is one of the most commonly used Pandas operations in real-world data analysis.

Salary Filter
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)
Name Salary 1 Priya 65000 3 Sneha 72000

Real-Time Example: Products Above ₹1000

Product Filtering
import pandas as pd

products = pd.DataFrame({
    "Product": ["Mouse", "Keyboard", "Monitor", "Laptop"],
    "Price": [800, 1500, 12000, 55000]
})

expensive = products[
    products["Price"] > 1000
]

print(expensive)
11. Multiple Conditions

Use & for AND conditions and | for OR conditions.

Multiple Filters
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)
Use parentheses around individual conditions when combining multiple Pandas conditions.
12. Adding a New Column

New columns can be created using existing columns.

New Column
import pandas as pd

employees = pd.DataFrame({
    "Name": ["Rahul", "Priya", "Amit"],
    "Salary": [40000, 50000, 60000]
})

employees["Annual Salary"] = employees["Salary"] * 12

print(employees)
Name Salary Annual Salary 0 Rahul 40000 480000 1 Priya 50000 600000 2 Amit 60000 720000
13. Updating a Column
Salary Increment
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.

14. Sorting Data

Use sort_values() to sort a DataFrame.

Sort by Salary
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)
Name Salary 3 Sneha 72000 1 Priya 65000 0 Rahul 45000 2 Amit 40000
15. Basic Statistical Analysis

Pandas provides convenient functions for analyzing numerical columns.

Statistics
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())
Total: 92000 Average: 18400.0 Minimum: 12000 Maximum: 25000 Median: 18000.0
16. describe() Function

The describe() method provides a statistical summary of numerical columns.

describe()
import pandas as pd


df = pd.DataFrame({
"Age": [22, 25, 28, 31, 35, 40],
"Salary": [30000, 40000, 50000, 60000, 70000, 90000]
})

print(df.describe())
▶ Output
             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.

17. Handling Missing Values

Real-world datasets often contain missing values. Pandas provides isnull(), dropna(), and fillna().

Detect Missing Values
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

fillna()
df["Age"] = df["Age"].fillna(
    df["Age"].mean()
)

df["Salary"] = df["Salary"].fillna(0)

print(df)
18. Removing Missing Rows
dropna()
import pandas as pd

df = pd.DataFrame({
    "Name": ["Rahul", "Priya", "Amit"],
    "Salary": [45000, None, 60000]
})

clean_data = df.dropna()

print(clean_data)
Important: dropna() removes rows containing missing values. In real applications, decide whether removing the row or filling the value is more appropriate for your dataset.
19. Removing Duplicate Data

Use drop_duplicates() to remove duplicate rows.

Duplicate Records
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)
Name City 0 Rahul Pune 1 Priya Mumbai 3 Amit Delhi
20. Renaming Columns
rename()
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)
21. Reading CSV Files

CSV files are commonly used to store tabular business data. Pandas can read them using read_csv().

Read CSV
import pandas as pd

df = pd.read_csv("employees.csv")

print(df.head())

Real-Time Example: Sales CSV

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
Python
import pandas as pd

sales = pd.read_csv("sales.csv")

print(sales)
22. Writing Data to CSV

Use to_csv() to save a DataFrame as a CSV file.

Export CSV
import pandas as pd

df = pd.DataFrame({
    "Name": ["Rahul", "Priya", "Amit"],
    "Salary": [45000, 55000, 60000]
})

df.to_csv("employees.csv", index=False)
index=False prevents Pandas from writing the DataFrame index as an additional CSV column.
23. Reading Excel Files

Pandas can also work with Excel files.

Read Excel
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.

24. Writing Data to Excel
Export Excel
import pandas as pd

df = pd.DataFrame({
    "Product": ["Laptop", "Mouse", "Keyboard"],
    "Sales": [55000, 1200, 2500]
})

df.to_excel(
    "sales_report.xlsx",
    index=False
)
25. GroupBy

groupby() is one of the most important Pandas features for business data analysis.

Sales by Department
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)
Department Clothing 55000 Electronics 155000 Name: Sales, dtype: int64
26. GroupBy with Multiple Aggregations
Business Summary
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)
▶ Output
             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.

27. Counting Values

value_counts() counts how frequently each value occurs.

Customer Cities
import pandas as pd

customers = pd.DataFrame({
    "City": [
        "Pune",
        "Mumbai",
        "Pune",
        "Delhi",
        "Pune",
        "Mumbai"
    ]
})

print(customers["City"].value_counts())
City Pune 3 Mumbai 2 Delhi 1 Name: count, dtype: int64
28. Applying Functions with apply()

The apply() method can apply a function to values in a Series or DataFrame.

Salary Category
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)
Name Salary Category 0 Rahul 35000 Low 1 Priya 55000 Medium 2 Amit 85000 High
29. Combining DataFrames with concat()

Use pd.concat() to combine DataFrames.

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)
▶ Output
  Employee Region


0 Rahul North
1 Priya North
2 Amit South
3 Sneha South
30. Merging DataFrames

merge() is useful when information is stored across multiple tables.

Customer and Order Data
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)
CustomerID Name Amount 0 1 Rahul 5000 1 2 Priya 7500 2 3 Amit 3200

This is similar to combining related tables using a common key.

31. Working with Dates

Pandas provides powerful tools for working with dates and time-series data.

Datetime
import pandas as pd


dates = pd.to_datetime([
"2026-01-01",
"2026-02-15",
"2026-03-20"
])

print(dates)
▶ Output
DatetimeIndex(['2026-01-01', '2026-02-15', '2026-03-20'],
          dtype='datetime64[ns]', freq=None)

Real-Time Example: Order Dates

Order Analysis
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)
▶ Output
   OrderDate  Amount  Month


0 2026-01-10 5000 1
1 2026-02-15 7500 2
2 2026-03-20 9000 3
32. Real-Time Example: Employee Management

Pandas can be used to analyze employee information stored in a structured table.

Employee Analysis
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()
)
▶ Output
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
33. Real-Time Example: E-Commerce Sales Analysis

An e-commerce company can use Pandas to understand product sales and revenue.

E-Commerce Analysis
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()
)
▶ Output
    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
This pattern is commonly used to transform transaction data into business reports.
34. Real-Time Example: Customer Analysis
Customer Data
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()
)
▶ Output
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
35. Real-Time Example: Inventory Management

Pandas can identify products that require restocking.

Inventory
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"]
]
)
▶ Output
    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
36. Real-Time Example: Student Result Analysis
Student Results
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
)
)
▶ Output
    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
37. Real-Time Example: Monthly Sales Report

This example demonstrates a common reporting workflow: load data, calculate revenue, group data, and generate a summary.

Monthly Sales Report
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())
▶ Output
Monthly Revenue:


Month
February 177000
January 118000
March 280000
Name: Revenue, dtype: int64

Total Revenue:
575000
38. Useful Pandas Functions
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
39. Pandas DataFrame vs Series
Feature Series DataFrame
Dimensions 1D 2D
Looks like Single column Table
Example pd.Series() pd.DataFrame()
Common use Single column of data Complete dataset
40. Complete Real-Time Example: Business Sales Analysis

This example combines several important Pandas concepts into one realistic business workflow.

Complete Business Example
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)
▶ Output
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
This demonstrates a typical Pandas workflow: load data → clean data → transform data → calculate values → group data → sort data → generate business insights.
41. Pandas Best Practices
  • Use import pandas as pd as the standard import.
  • Inspect data using head(), tail(), and info().
  • 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() or to_excel().
42. Important Pandas Rules
  • Pandas is primarily used for data manipulation and analysis.
  • Series represents one-dimensional labeled data.
  • DataFrame represents two-dimensional tabular data.
  • Use loc for label-based selection.
  • Use iloc for position-based selection.
  • Use boolean conditions to filter rows.
  • Use groupby() to summarize data by categories.
  • Use fillna() or dropna() 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() and read_excel() to load external data.
  • Use to_csv() and to_excel() to export processed data.
  • Pandas works especially well with NumPy for numerical data processing.
Python Pandas

DataFrames, data cleaning, filtering, grouping, reporting, and real-time data analysis.