Programming in Python
Chapter 4: File Handling using Panda Library
Comprehensive study guide exploring Python file handling, reading and writing CSV files using Pandas, and fully solved textbook exercises.
Welcome to Chapter Four! In this section of our Python journey, we will interact directly with the files on your computer.
File handling empowers programs to read input datasets and save outputs permanently. We will explore native Python file operations, modes of access, and leverage the powerful Pandas library to effortlessly manage CSV files. Below, you will also find complete textbook exercise solutions for your Class 10 SEE preparation.
1.File Handling using Panda
4.6.1 Concept of File Handling in Python
File handling involves interacting directly with the files on your computer’s hard drive to read data from them or write new data into them.
Benefits of File Handling in Python:
Difficulties of File Handling in Python:
4.6.2 Modes of File Handling
To do anything with a file in Python, the very first step is to open it. We use Python’s built-in open() function for this.
Syntax:
with open('Filename.txt', 'mode') as file:
# Perform file operations here
Access Modes in Python
When opening a file, you must tell Python how you intend to use it. Here are the core modes:
| Mode | Name | Behavior |
|---|---|---|
"r" |
Read-only | Allows you to read the file. You cannot change it. Raises an error if the file does not exist. |
"w" |
Write-only | Allows you to write data. Creates the file if it doesn’t exist. If the file already has data, it erases and overwrites everything! |
"a" |
Append-only | Allows you to add new data to the very end of the file. Creates the file if it doesn’t exist. It does not erase existing data. |
"x" |
Create | Creates a brand new, empty file. It throws an error if a file with that name already exists. |
1. Reading a File
To read a file, it must be opened in "r" mode. Python offers a few ways to read the content:
file.read(): Reads the entire content of the file all at once.file.read(10): Reads only the first 10 characters of the file.file.readline(): Reads only the very first line.file.readlines(): Reads all the lines and returns them as a list.2. Creating and Writing to a File
When you open a file in "w" mode, you can use file.write() to insert text. Remember, "w" will completely erase whatever was in the file previously!
file = open("Test.txt", "w")
file.write("Hello, World!")
file.close()
3. Appending to a File
If you want to add text without destroying the old data, use "a" (Append) mode.
write(): Adds a single string to the end of the file.writelines(): Allows you to insert multiple strings (like a list of lines) in one go.4. Closing a File
Once you are done reading or writing, you must close the file using file.close(). Closing the file terminates all active resources, ensures your data is saved properly, and frees up system memory.
4.6.3 Reading and Writing CSV Files (Using Pandas)
CSV stands for Comma Separated Values. It is the most common format for exporting spreadsheets and databases. A CSV is simply a plain text file where every piece of data is separated by a comma.
Why are CSVs so popular?
Using the Pandas Library
While Python has a built-in csv module, Pandas makes data importing and analyzing phenomenally easier. Pandas builds on other packages to give us a single, convenient toolkit for data analysis.
To use Pandas, you must first install it using your terminal: $ pip install pandas
1. Reading a CSV with Pandas
We use the pd.read_csv() function to instantly load a CSV file into a powerful table structure called a DataFrame.
import pandas as pd
# Load the file into a DataFrame
data = pd.read_csv("Salary_Data.csv", delimiter=',')
# Display the contents
print(data)
Helpful DataFrame Commands:
data.columns: Extracts and displays just the header/field names of the table.data.Salary: Extracts and displays all the rows for the specific “Salary” column.data.info(): Shows a summary of the DataFrame (data types, missing values).data.shape: Tells you the exact size of the table (rows, columns).2. Writing to a CSV with Pandas
If you have data inside Python and want to save it as a brand new CSV file, you use the to_csv() method.
import pandas as pd
# 1. Create a dictionary of data
data = {
'Product': ['Laptop', 'Smartphone', 'Tablet'],
'Price': [75000, 15000, 20000]
}
# 2. Convert it into a Pandas DataFrame
df = pd.DataFrame(data)
# 3. Save it as a CSV file (index=False prevents Pandas from writing row numbers)
df.to_csv('products.csv', sep=',', index=False)
Exercise 1: Choose the correct answer.
Select an option to view the correct answer and justification.
Justification: Pandas provides powerful, high-level data structures like DataFrames that make reading, writing, and analyzing structured CSV files incredibly simple.
Justification:
read_csv() is the standard Pandas function utilized to load a CSV file directly into a DataFrame.Justification: The parameter
delimiter (or sep) tells Pandas exactly which character is being used to separate the columns of data in the text file.Justification: Calling
data.columns on a DataFrame returns an index of all the header/field names present in the table.Justification: The
to_csv() method is called directly on an existing DataFrame object (like df) to export its contents into a standard CSV file.Justification: By default, Pandas will write the row numbers (0, 1, 2…) into the file. Setting
index=False prevents these unnecessary index numbers from being saved.Justification: CSV literally stands for “Comma Separated Values,” making the comma the universal default delimiter.
Justification: The
"r" mode stands for Read-only. It allows a program to view file contents without risking accidental modifications.Justification: The
"w" (Write) mode completely erases existing data upon opening and allows you to write entirely fresh content.Justification: The
pd.DataFrame() method constructs a tabular DataFrame object in memory out of standard Python data structures like lists or dictionaries.Exercise 2: Write short answers to these questions.
import pandas as pd at the very top of your file. (Note: If it is not installed on your computer, you must first run pip install pandas in your terminal).
read_csv() function. You assign the result to a variable to hold the DataFrame: data = pd.read_csv('filename.csv', delimiter=',')
Example: If your DataFrame is named
data and you want the salary column, you type: data.Salary.
to_csv() method directly to your DataFrame object. You specify the desired filename and typically set index=False to avoid writing row numbers. Syntax:
df.to_csv('filename.csv', sep=',', index=False)
FileNotFoundError (crash), because read mode requires the file to already exist on your system before it can be opened.
"w") mode will brutally erase and overwrite all existing content in the file. Append ("a") mode safely preserves existing content and simply adds new data to the very end of the file.
close() method terminates active resources, frees up system memory, and ensures that any data waiting in the memory buffer is completely and safely written to the hard disk without corruption.
Exercise 3: Long Answer Questions.
To read data from a CSV file using Pandas, you must follow these steps:
pip install pandas).pd alias.pd.read_csv() function, passing the target file name (and its file path if it isn’t in the same folder) as a string argument.# 1. Import the Pandas library
import pandas as pd
# 2. Read the CSV file and store it in a DataFrame
df = pd.read_csv("employee_data.csv")
# Optional: Print to verify it worked
print(df)
a. Read the data from the “student_data.csv” file into a DataFrame.
b. Print the first 5 rows of the DataFrame.
c. Calculate the average age of the students.
d. Create a new DataFrame containing only the students with a “Grade” of “A”. 4 Marks
import pandas as pd
# a. Read the data into a DataFrame
df = pd.read_csv("student_data.csv")
# b. Print the first 5 rows of the DataFrame
# (The .head() function automatically grabs the first 5 rows)
print("--- First 5 Rows ---")
print(df.head())
# c. Calculate the average age of the students
# (We access the 'Age' column and use the .mean() function)
average_age = df['Age'].mean()
print("\nAverage Age of Students:", average_age)
# d. Create a new DataFrame containing only students with an "A" Grade
# (We filter the DataFrame using a logical condition)
grade_a_students = df[df['Grade'] == 'A']
print("\n--- Students with Grade A ---")
print(grade_a_students)
📚 Also Read: Class 10 SEE Notes
Computer Science Units
Other Subjects
