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

Column Selection in Pandas

Learn how to select a single column, multiple columns, and specific groups of columns from a Pandas DataFrame.

1. What Is Column Selection?

Column selection means retrieving one or more columns from a DataFrame. It is useful when you want to work with only the information required for a particular task.

For example, Samadhan may need student names for an attendance report, while Pradnya may need names and marks to prepare a performance report. Pandas lets us select those columns without displaying the entire dataset.

Create a Sample DataFrame

import pandas as pd

df = pd.DataFrame({
    "Name": ["Samadhan", "Pradnya", "Samiksha", "Amit"],
    "Age": [21, 22, 20, 23],
    "City": ["Pune", "Mumbai", "Pune", "Nashik"],
    "Marks": [85, 92, 78, 88]
})

print(df)

Output

       Name  Age    City  Marks
0  Samadhan   21    Pune     85
1   Pradnya   22  Mumbai     92
2  Samiksha   20    Pune     78
3      Amit   23  Nashik     88

2. Select a Single Column

The simplest way to select one column is to use its name inside square brackets.

names = df["Name"]

print(names)

Output

0    Samadhan
1     Pradnya
2    Samiksha
3        Amit
Name: Name, dtype: object

The result is a Pandas Series, which is a one-dimensional labeled data structure.

Remember: df["Name"] returns a Series, not a DataFrame.

Select another column

print(df["Marks"])

Output

0    85
1    92
2    78
3    88
Name: Marks, dtype: int64

3. Select Multiple Columns

To select more than one column, provide a list of column names inside the outer square brackets.

selected = df[["Name", "Marks"]]

print(selected)

Output

       Name  Marks
0  Samadhan     85
1   Pradnya     92
2  Samiksha     78
3      Amit     88

The result is a DataFrame because it contains multiple columns.

Common mistake: df["Name", "Marks"] is not the normal way to select two columns. Use df[["Name", "Marks"]] instead.

4. Visual Guide: Single vs Multiple Columns

A single column selection returns a Series, whereas a list of column names returns a DataFrame containing those columns.

Single Column Multiple Columns Name Samadhan Pradnya Samiksha Amit Name Marks Samadhan 85 Pradnya 92 Samiksha 78 Amit 88 df["Name"] → Series df[["Name", "Marks"]] → DataFrame

Figure 1: The number of selected columns determines whether the result is a Series or a DataFrame.

5. Select Columns in a Different Order

You can specify column names in any order. Pandas returns the selected columns in the order you provide.

print(df[["Marks", "Name"]])

Output

   Marks      Name
0     85  Samadhan
1     92   Pradnya
2     78  Samiksha
3     88      Amit
Tip: Choose the column order that makes your report or analysis easier to read.

6. Select Columns and Calculate Statistics

After selecting a column, you can apply Series methods such as mean(), max(), and min().

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

Output

Average marks: 85.75
Highest marks: 92
Lowest marks: 78

Statistics for multiple columns

print(df[["Age", "Marks"]].mean())

Output

Age      21.50
Marks    85.75
dtype: float64

7. Select Columns Using a Variable

If the required columns are stored in a Python list, you can pass that list to the DataFrame.

columns_to_select = ["Name", "City", "Marks"]

result = df[columns_to_select]

print(result)

Output

       Name    City  Marks
0  Samadhan    Pune     85
1   Pradnya  Mumbai     92
2  Samiksha    Pune     78
3      Amit  Nashik     88

This technique is useful when a report or analysis needs different columns depending on the user's choice.

8. Check Available Column Names

Before selecting columns, you can inspect the column names using df.columns.

print(df.columns)

Output

Index(['Name', 'Age', 'City', 'Marks'], dtype='object')

To get the column names as a regular Python list, use tolist().

print(df.columns.tolist())

Output

['Name', 'Age', 'City', 'Marks']

9. Visual Guide: Selecting Columns for a Report

Imagine that you want to prepare a marks report. The original DataFrame has four columns, but the report only requires Name and Marks.

Original DataFrame Selected Report Name Age City Marks Samadhan 21 Pune 85 Pradnya 22 Mumbai 92 Samiksha 20 Pune 78 Amit 23 Nashik 88 Name Marks Samadhan 85 Pradnya 92 Samiksha 78 Amit 88 df[["Name", "Marks"]]

Figure 2: Selecting only the required columns creates a focused DataFrame for reporting.

10. Common Errors and Best Practices

Error 1: Incorrect column name

df["marks"]

If the actual column is named Marks, using marks may raise a KeyError. Column labels are case-sensitive.

Correct code:

df["Marks"]

Error 2: Forgetting the list for multiple columns

Use a list of names inside the outer brackets:

df[["Name", "Marks"]]

Error 3: Selecting a column that does not exist

Inspect df.columns to check the available column names before selecting them.

Best practice: Use explicit column names when selecting data for reports and calculations. It makes your code easier to understand and maintain.

11. Practice Exercises

Exercise 1: Basic Selection

  1. Select only the Name column.
  2. Select only the Marks column.
  3. Select the Name and City columns.
  4. Select the City and Marks columns.

Exercise 2: Column Order

  1. Display Marks before Name.
  2. Display City, Name, and Age in that order.
  3. Store the selected column names in a list and use that list.

Exercise 3: Calculations

  1. Calculate the average marks.
  2. Find the maximum marks.
  3. Calculate the average Age and Marks together.
  4. Print all available column names.

Suggested solution

print(df["Name"])
print(df["Marks"])

print(df[["Name", "City"]])
print(df[["City", "Marks"]])

print(df[["Marks", "Name"]])
print(df[["City", "Name", "Age"]])

selected_columns = ["Name", "Marks"]
print(df[selected_columns])

print(df["Marks"].mean())
print(df["Marks"].max())
print(df[["Age", "Marks"]].mean())
print(df.columns.tolist())

12. Revision Questions

  1. What is column selection in Pandas?
  2. How do you select a single column?
  3. What is the difference between a Series and a DataFrame?
  4. How do you select multiple columns?
  5. How can you change the order of selected columns?
  6. How do you inspect the column names of a DataFrame?
  7. What happens if you select a column that does not exist?
  8. How can you calculate the mean of a selected column?

Summary

Column selection allows you to retrieve only the columns required for a task. Use df["Name"] to select a single column and df[["Name", "Marks"]] to select multiple columns.

A single column selection normally returns a Series, while multiple-column selection returns a DataFrame. You can also store column names in a list, change their display order, inspect available labels, and calculate statistics on selected data.