Importing & Exporting CSV Files using Pandas | read_csv() and to_csv() | CBSE Class 12 Informatics Practices (2026–27)
Class 12 · Informatics Practices
Importing & Exporting CSV Files using Pandas
One of the most useful features of the Pandas library is its ability to read data from external files and save processed data back to files. The most commonly used file format is the CSV (Comma-Separated Values) file.
CSV files are widely used in schools, businesses, hospitals, banks, and government organizations because they are simple, lightweight, and supported by almost every spreadsheet and database application.
What is a CSV File?
A CSV (Comma-Separated Values) file is a text file in which data is stored in tabular form. Each row represents a record, and each value in a row is separated by a comma.
Example of a CSV File
student.csv
Name,Marks,City Amit,85,Jaipur Neha,92,Delhi Rohan,78,Mumbai Priya,88,Jaipur
Advantages of CSV Files
- Easy to create and edit.
- Supported by Microsoft Excel, Google Sheets, and databases.
- Small file size.
- Easy to exchange data between different software.
- Can be read directly using Pandas.
Importing a CSV File
To import data from a CSV file into a DataFrame, use the read_csv() function.
Syntax
import pandas as pd
df = pd.read_csv("filename.csv")
Example 1
import pandas as pd
df = pd.read_csv("student.csv")
print(df)
Output
Name Marks City
0 Amit 85 Jaipur
1 Neha 92 Delhi
2 Rohan 78 Mumbai
3 Priya 88 Jaipur
Viewing the First Records
print(df.head())
Displays the first five rows by default.
Viewing the Last Records
print(df.tail())
Displays the last five rows by default.
Displaying Specific Columns
print(df["Marks"])
Displaying Multiple Columns
print(df[["Name","City"]])
Exporting Data to a CSV File
After processing data, it can be saved back into a CSV file using the to_csv() function.
Syntax
DataFrame.to_csv("filename.csv")
Example
import pandas as pd
data = {
"Name":["Amit","Neha","Rohan"],
"Marks":[85,92,78]
}
df = pd.DataFrame(data)
df.to_csv("result.csv")
The file result.csv is created in the current working directory.
The index Parameter
By default, Pandas also saves the row index in the CSV file.
df.to_csv("result.csv")
Saved file:
,Name,Marks 0,Amit,85 1,Neha,92 2,Rohan,78
Saving Without Index
df.to_csv("result.csv", index=False)
Output File:
Name,Marks Amit,85 Neha,92 Rohan,78
The index=False parameter prevents the row index from being saved.
The header Parameter
The header parameter controls whether column names are written to the CSV file.
df.to_csv(
"result.csv",
header=False,
index=False
)
Output:
Amit,85 Neha,92 Rohan,78
Import → Modify → Export Workflow
CSV File
│
▼
read_csv()
│
▼
DataFrame
│
Modify / Filter / Update
│
▼
to_csv()
│
▼
New CSV File
Real-Life Applications
| Field | Example |
|---|---|
| School | Student Result Records |
| Hospital | Patient Details |
| Bank | Customer Transactions |
| Business | Sales Reports |
| Sports | Player Statistics |
Common Errors
| Error | Reason |
|---|---|
| FileNotFoundError | CSV file does not exist. |
| PermissionError | File is already open in another application. |
| ParserError | Incorrect CSV format. |
| UnicodeDecodeError | Wrong file encoding. |
Difference Between read_csv() and to_csv()
| read_csv() | to_csv() |
|---|---|
| Imports data | Exports data |
| Creates a DataFrame | Saves a DataFrame |
| Reads from disk | Writes to disk |
Quick Revision
| Function | Purpose |
|---|---|
| read_csv() | Read CSV file |
| to_csv() | Save CSV file |
| index=False | Do not save row index |
| header=False | Do not save column names |
| CSV | Comma-Separated Values |
CBSE Exam Tips
- Remember that
read_csv()imports data into a DataFrame. - Use
to_csv()to save a DataFrame. - Remember the purpose of
index=False. - Know the purpose of
header=False. - Be able to identify the output of CSV-related programs.
- Practice reading, modifying, and exporting CSV files for practical examinations.
Summary
CSV files are one of the most common formats used for storing tabular data. Pandas provides the read_csv() function to import CSV files into a DataFrame and the to_csv() function to export processed data back to CSV files. Understanding these functions is essential for the CBSE practical examination and real-world data analysis tasks.