Master Pandas From Scratch

Learn Pandas fundamentals, Series, DataFrames, data selection, filtering, cleaning, sorting, grouping, merging, reshaping, string operations, time series, data analysis, performance optimization, visualization, and practical projects through structured lessons.

COURSE SYLLABUS
1. Pandas Introduction and Environment Setup
Introduction to Pandas Installation and Environment Setup
2. Pandas Core Data Structures
Data Structures Overview Pandas Series DataFrames Indexes, Rows and Columns Shape, Size and Dimensions
3. Pandas Data Input and Output
Data Input and Output Overview Reading CSV, Excel and JSON Files Writing and Exporting Files
4. Initial Data Inspection
Data Inspection Overview Head and Tail Data Info and Types Descriptive Statistics
5. Selection and Indexing
Selection and Indexing Overview Column Selection loc and iloc Index Management
6. Filtering and Conditional Selection
Filtering Overview Boolean Filtering Multiple Conditions isin, between and str.contains
7. Basic Data Cleaning
Data Cleaning Overview Missing Values Duplicate Data Rename and Replace Data Type Conversion
8. Vectorized Operations and Sorting
Operations and Sorting Overview Column Operations Sorting Values and Indexes Sum, Mean, Max and Min
9. GroupBy and Aggregation
GroupBy Overview Grouping Data Aggregation Functions Group Filtering Group Transform
10. Merging, Joining and Concatenating
Merge, Join and Concat Overview Concatenation Merging DataFrames Joining by Index
11. Reshaping and Pivoting
Reshaping Overview Pivot Tables Melting Data Stack and Unstack Cross Tabulation
12. String and Categorical Data
String and Categorical Overview String Operations Categorical Data One-Hot Encoding
13. Time Series Analysis
Time Series Overview DateTime Operations Time-Based Indexes Resampling Data Shift and Rolling Windows
14. Custom Functions and Mapping
Apply and Mapping Overview Apply and Lambda Functions Map Operations Element-Wise Operations
15. Performance and Large Datasets
Performance Overview Memory Optimization Processing Files in Chunks PyArrow, Dask and Polars
16. Pandas Data Visualization
Visualization Overview Basic Plotting Histograms, Lines, Scatter and Box Plots Matplotlib and Seaborn

Pandas Series

Learn how to create a Series, assign custom indexes, access values, perform calculations, and work with labelled one-dimensional data.

1. Learning Objectives

After completing this lesson, you will be able to:

  • Explain what a Pandas Series is.
  • Create a Series from lists and dictionaries.
  • Use default and custom indexes.
  • Access Series values using labels and positions.
  • Inspect values, indexes, and data types.
  • Perform calculations and basic filtering.

2. What Is a Pandas Series?

A Series is a one-dimensional labelled data structure provided by Pandas. It contains a sequence of values and an associated index used to identify each value.

A Series is useful when working with a single set of related values, such as student marks, product prices, employee salaries, or daily temperatures.

Student Marks Series Index labels and corresponding values Samadhan Pradnya Samiksha 80 92 86 Each index label identifies its corresponding value.

Figure 1: A Series pairs index labels with values.

3. Creating a Series from a List

The pd.Series() function converts a supported sequence of values into a Series.

import pandas as pd

marks = pd.Series([80, 92, 86])

print(marks)

Output:

0    80
1    92
2    86
dtype: int64

Understanding the Output

  • 0, 1, 2 are the default index labels.
  • 80, 92, 86 are the stored values.
  • dtype: int64 indicates the integer data type on this example's environment.
Tip: Pandas usually creates a default integer index when you do not provide one explicitly.

4. Creating a Series with Custom Indexes

You can assign meaningful labels by passing an index argument. This makes it easier to access a value using a label rather than a default number.

import pandas as pd

marks = pd.Series(
    [80, 92, 86],
    index=["Samadhan", "Pradnya", "Samiksha"]
)

print(marks)

Output:

Samadhan    80
Pradnya     92
Samiksha    86
dtype: int64

Access a Value Using Its Label

print(marks["Pradnya"])

Output:

92
Important: Index labels must be unique if you want each label to identify exactly one entry. Pandas does allow duplicate labels, but label-based selection can then return multiple values.

5. Creating a Series from a Dictionary

A Python dictionary can also be used to create a Series. Its keys become the index labels and its values become the Series data.

import pandas as pd

student_marks = {
    "Samadhan": 80,
    "Pradnya": 92,
    "Samiksha": 86
}

marks = pd.Series(student_marks)

print(marks)

Output:

Samadhan    80
Pradnya     92
Samiksha    86
dtype: int64

This is convenient when your data already consists of meaningful keys and corresponding values.

6. Accessing Series Values

You can access values using index labels or positional selection. These approaches serve different purposes.

Label-Based Selection

print(marks.loc["Samadhan"])
print(marks.loc["Samiksha"])

Output:

80
86

Position-Based Selection

print(marks.iloc[0])
print(marks.iloc[1])

Output:

80
92
Method Purpose Example
loc Select by index label. marks.loc["Pradnya"]
iloc Select by integer position. marks.iloc[1]
Remember: loc uses labels, while iloc uses zero-based integer positions.

7. Inspecting a Series

A Series has attributes and methods that help you inspect its values, index, data type, and summary statistics.

print("Values:", marks.values)
print("Index:", marks.index)
print("Data type:", marks.dtype)
print("Dimensions:", marks.ndim)
print("Size:", marks.size)
print("Shape:", marks.shape)

Example output:

Values: [80 92 86]
Index: Index(['Samadhan', 'Pradnya', 'Samiksha'], dtype='object')
Data type: int64
Dimensions: 1
Size: 3
Shape: (3,)

The exact representation of an index and data type can vary with the Pandas version and the values used.

8. Calculations with a Series

Numerical Series support common calculations such as total, average, minimum, and maximum.

print("Total:", marks.sum())
print("Average:", marks.mean())
print("Highest:", marks.max())
print("Lowest:", marks.min())

Output:

Total: 258
Average: 86.0
Highest: 92
Lowest: 80
Method Meaning
sum() Adds the values.
mean() Calculates the average.
max() Returns the maximum value.
min() Returns the minimum value.

9. Filtering a Series

Boolean conditions let you select values that satisfy a requirement. For example, you can find students whose marks are greater than or equal to 85.

high_marks = marks[marks >= 85]

print(high_marks)

Output:

Pradnya     92
Samiksha    86
dtype: int64

The condition creates a Boolean mask. Pandas uses that mask to keep only the entries whose condition is true.

10. Handling Missing Values

A Series can contain missing values. Pandas provides methods to identify and handle them.

import pandas as pd

marks = pd.Series({
    "Samadhan": 80,
    "Pradnya": None,
    "Samiksha": 86
})

print(marks.isna())

Example output:

Samadhan    False
Pradnya      True
Samiksha    False
dtype: bool

Fill Missing Values

filled_marks = marks.fillna(0)

print(filled_marks)

fillna(0) creates a Series where missing values are filled with zero. Choose a replacement that makes sense for your data; zero may not always be appropriate.

11. Practical Exercise

CIIT Training Institute: Marks Analysis

Create a Series containing the marks of Samadhan, Pradnya, and Samiksha.

Complete these tasks:

  1. Assign student names as index labels.
  2. Print Pradnya's marks.
  3. Display the average marks.
  4. Find the highest and lowest marks.
  5. Filter students with marks of 85 or above.

Practice Code

import pandas as pd

marks = pd.Series(
    [80, 92, 86],
    index=["Samadhan", "Pradnya", "Samiksha"]
)

print("Pradnya:", marks.loc["Pradnya"])
print("Average:", marks.mean())
print("Highest:", marks.max())
print("Lowest:", marks.min())

print("\nMarks >= 85:")
print(marks[marks >= 85])

Revision Questions

  1. What is a Pandas Series?
  2. How do you create a Series from a list?
  3. What is a custom index?
  4. What is the difference between loc and iloc?
  5. Which methods calculate the sum and mean?
  6. How can you identify missing values?

Summary

A Pandas Series is a one-dimensional labelled data structure that stores values alongside an index. You can create one from a list or dictionary and provide custom index labels.

You learned to access values using loc and iloc, inspect the Series using attributes, perform calculations, filter entries, and identify or fill missing values.