ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Essential Numpy and Pandas
Chapter 1: Introduction to Data Handling in Python
What are Numpy and Pandas?
Importance for AI and Data Science
Setting Up Your Environment
Running Your First Code Snippets
Introduction to Jupyter Notebooks
Hands-on practical: Setup and Verification
Quiz for Chapter 1
Chapter 2: Getting Started with NumPy Arrays
Understanding NumPy N-dimensional Arrays
Creating Arrays from Python Lists
Built-in Array Creation Functions
Understanding Array Data Types
Basic Array Attributes
Hands-on practical: Creating and Inspecting Arrays
Quiz for Chapter 2
Chapter 3: NumPy Array Indexing and Slicing
Accessing Single Elements
Slicing 1D Arrays
Slicing 2D Arrays
Boolean Indexing
Fancy Indexing
Modifying Array Subsets
Hands-on practical: Selecting Data from Arrays
Quiz for Chapter 3
Chapter 4: Fundamental NumPy Operations
Basic Arithmetic Operations
Introduction to Universal Functions (ufuncs)
Mathematical and Statistical Functions
Logical Operations on Arrays
Introduction to Broadcasting
Hands-on practical: Performing Calculations on Arrays
Quiz for Chapter 4
Chapter 5: Introduction to Pandas
What is Pandas?
Pandas Data Structure: Series
Creating Series
Pandas Data Structure: DataFrame
Creating DataFrames
Inspecting DataFrames
Hands-on practical: Creating and Examining Series/DataFrames
Quiz for Chapter 5
Chapter 6: Loading and Saving Data with Pandas
Reading Data from CSV Files
Reading Data from Excel Files
Reading Data from Other Formats
Writing Data to CSV Files
Writing Data to Excel Files
Hands-on practical: Importing and Exporting Datasets
Quiz for Chapter 6
Chapter 7: Data Selection and Indexing in Pandas
Selecting Columns
Selecting Rows Using Labels (.loc)
Selecting Rows Using Integer Position (.iloc)
Mixing Label and Position Based Indexing
Conditional Selection (Boolean Indexing)
Setting DataFrame Index
Resetting DataFrame Index
Hands-on practical: Accessing Specific Data Subsets
Quiz for Chapter 7
Chapter 8: Basic Data Manipulation with Pandas
Detecting Missing Data
Handling Missing Data: Dropping
Handling Missing Data: Filling
Dropping Columns and Rows
Adding New Columns
Modifying Existing Columns
Renaming Columns
Sorting Data
Hands-on practical: Cleaning and Modifying DataFrames
Quiz for Chapter 8
Chapter 9: Grouping and Aggregating Data
The Split-Apply-Combine Concept
Grouping Data with groupby()
Applying Aggregation Functions
Applying Multiple Aggregations
Grouping by Multiple Columns
Iterating Through Groups
Hands-on practical: Summarizing Data with GroupBy
Quiz for Chapter 9
Chapter 10: Combining DataFrames
Introduction to Combining Data
Concatenating DataFrames (pd.concat)
Database-Style Merging (pd.merge)
Understanding Merge Types (Joins)
Merging on Index
Index-Based Joining (.join)
Hands-on practical: Combining Datasets
Quiz for Chapter 10

Hands-on practical: Importing and Exporting Datasets

Common scenarios of reading data from files, making a minor adjustment, and then saving the results are demonstrated. This simulates a frequent pattern in data analysis: load, process, save.

First, ensure you have Pandas imported. Typically, we use the alias pd.

import pandas as pd
print(f"Pandas version: {pd.__version__}")

Preparing Sample Data Files

For these exercises, we need some data files. Let's imagine we have two files:

  1. students.csv: A Comma Separated Value file.
  2. grades.xlsx: A Microsoft Excel file.

If you are running this code locally, you can create these files yourself.

Create students.csv: Save the following text into a file named students.csv in the same directory as your script or notebook:

StudentID,Name,Major
101,Alice,Physics
102,Bob,Chemistry
103,Charlie,Mathematics
104,David,Physics

Create grades.xlsx: You'll need a library like openpyxl installed (pip install openpyxl) to work with .xlsx files. Create an Excel file named grades.xlsx with the following data in a sheet named Sheet1:

StudentID Course Grade
101 Mechanics 85
102 Org Chem 92
101 E&M 78
103 Calculus 95
102 Thermo 88
104 Mechanics 81

Reading Data from CSV

Now, let's read the students.csv file into a Pandas DataFrame using pd.read_csv().

# Read the CSV file
students_df = pd.read_csv('students.csv')

# Display the DataFrame
print("Students DataFrame:")
print(students_df)

You'll notice Pandas correctly identified the first row as the header and used a default integer index (0, 1, 2, 3).

What if our file used a different separator, like a tab? Or what if we wanted the StudentID column to be the DataFrame's index? We can use parameters in read_csv.

# Example: Reading assuming StudentID should be the index
students_df_indexed = pd.read_csv('students.csv', index_col='StudentID')

print("\nStudents DataFrame with StudentID as index:")
print(students_df_indexed)

Setting index_col='StudentID' tells Pandas to use the values from that column as the row labels (the index) for the DataFrame.

Reading Data from Excel

Next, let's load the data from the grades.xlsx file using pd.read_excel().

# Read the Excel file (ensure openpyxl or another engine is installed)
try:
    grades_df = pd.read_excel('grades.xlsx')
    print("\nGrades DataFrame (from Excel Sheet1):")
    print(grades_df)
except ImportError:
    print("\nPlease install 'openpyxl' to read Excel files: pip install openpyxl")
except FileNotFoundError:
    print("\nMake sure 'grades.xlsx' is in the correct directory.")

By default, read_excel reads the first sheet. If our data was on a different sheet, say "CourseGrades", we would specify it:

# Example: Reading a specific sheet (if it existed)
# grades_df_specific_sheet = pd.read_excel('grades.xlsx', sheet_name='CourseGrades')

You can use the sheet name (string) or its zero-based index (integer) for sheet_name.

Simple Modification

Let's take the grades_df we loaded and add a new column. Suppose we want a 'Status' column indicating 'Pass' if the grade is 70 or above, and 'Fail' otherwise. We can achieve this using boolean indexing and assignment.

# Check if grades_df was loaded successfully before proceeding
if 'grades_df' in locals():
    # Add a 'Status' column, default to 'Fail'
    grades_df['Status'] = 'Fail'

    # Update 'Status' to 'Pass' where Grade >= 70
    grades_df.loc[grades_df['Grade'] >= 70, 'Status'] = 'Pass'

    print("\nGrades DataFrame with Status column added:")
    print(grades_df)
else:
    print("\nSkipping modification as grades_df was not loaded.")

We first created the column and assigned a default value ('Fail'). Then, we used .loc with a boolean condition (grades_df['Grade'] >= 70) to select the rows where the grade met the passing threshold and updated only their 'Status' value to 'Pass'.

Writing Data to CSV

Now that we have modified grades_df, let's save it back to a new CSV file named processed_grades.csv.

A common requirement is to not write the DataFrame's index (0, 1, 2...) into the file, as it often doesn't represent meaningful data. We use the index=False parameter for this.

# Check if grades_df exists before saving
if 'grades_df' in locals():
    # Save the modified DataFrame to CSV without the index
    grades_df.to_csv('processed_grades.csv', index=False)
    print("\nSaved modified grades data to processed_grades.csv (index excluded).")

    # You can verify by reading it back or opening the file
    # check_csv = pd.read_csv('processed_grades.csv')
    # print("\nVerification read from processed_grades.csv:")
    # print(check_csv)
else:
    print("\nSkipping CSV export as grades_df is not available.")

If you open processed_grades.csv, you'll see the columns StudentID, Course, Grade, and Status, but not the default 0-based index column.

Writing Data to Excel

Similarly, we can save the modified DataFrame to an Excel file. Let's call it processed_grades.xlsx and put the data in a sheet named 'Final Grades'. Again, we'll typically want index=False.

# Check if grades_df exists before saving
if 'grades_df' in locals():
    try:
        # Save the modified DataFrame to Excel without the index, specifying sheet name
        grades_df.to_excel('processed_grades.xlsx', sheet_name='Final Grades', index=False)
        print("\nSaved modified grades data to processed_grades.xlsx (Sheet: 'Final Grades', index excluded).")

        # Verification (optional)
        # check_excel = pd.read_excel('processed_grades.xlsx', sheet_name='Final Grades')
        # print("\nVerification read from processed_grades.xlsx:")
        # print(check_excel)
    except ImportError:
        print("\nPlease install 'openpyxl' to write Excel files: pip install openpyxl")
else:
    print("\nSkipping Excel export as grades_df is not available.")

This creates an Excel file with our processed data neatly organized in the specified sheet.

Summary

In this practice session, you used the fundamental Pandas IO functions:

  • pd.read_csv() to load data from text files, exploring index_col.
  • pd.read_excel() to load data from spreadsheets, exploring sheet_name.
  • .to_csv() to save DataFrames, emphasizing the use of index=False.
  • .to_excel() to save DataFrames, using index=False and sheet_name.

You also performed a simple data modification step between reading and writing, demonstrating a basic data processing workflow. Mastering these input and output operations is essential for integrating Pandas into your data analysis tasks.

© 2026 ApX Machine Learning