Importing and Exporting Data between CSV Files and DataFrames – Class 12 IP Notes

Working with data is an important task in programming. In Class 12 Informatics Practices, we use the Pandas library to handle data. One of the most common formats used to store and exchange data is CSV which stands for Comma Separated Values. Pandas provides simple functions to import data from a CSV file into a DataFrame and export data from a DataFrame back to a CSV file.
What is a CSV File
A CSV file is a plain text file where each line represents a record and values are separated by commas. For example a file named students.csv may look like this:
RollNo,Name,Marks
101,Anita,92
102,Rahul,85
103,Simran,78
Importing CSV File into a DataFrame
To load data from a CSV file into a Pandas DataFrame we use the read_csv function.
import pandas as pd
df = pd.read_csv("students.csv")
print(df)
Explanation:
- pd.read_csv(“filename.csv”) reads the CSV file into a DataFrame
- By default it assumes comma as the separator
Some useful parameters of read_csv are:
- sep → to specify a different separator, for example sep=”;”
- header → row number to use as column names, header=None if no header row exists
- index_col → to set a column as index
- usecols → to load only specific columns
Example:
df = pd.read_csv("students.csv", usecols=["RollNo", "Name"])
print(df)
Exporting DataFrame to CSV File
To save a DataFrame into a CSV file we use the to_csv function.
df.to_csv("output.csv", index=False)
Explanation:
- to_csv(“filename.csv”) saves the DataFrame to a CSV file
- index=False prevents writing row numbers into the file
Example Program
import pandas as pd
# Import data
df = pd.read_csv("students.csv")
print("Original Data:")
print(df)
# Add new column
df["Grade"] = ["A", "B", "C"]
# Export updated DataFrame
df.to_csv("students_updated.csv", index=False)
print("Updated data saved successfully")
The new file students_updated.csv will look like this:
RollNo,Name,Marks,Grade
101,Anita,92,A
102,Rahul,85,B
103,Simran,78,C
Summary
- read_csv is used to import data from a CSV file into a DataFrame
- to_csv is used to export a DataFrame to a CSV file
- CSV is a simple and widely used file format for storing and exchanging data
Short Revision Table – Importing and Exporting CSV in Pandas
Operation | Function / Parameter | Example Code |
---|---|---|
Import CSV into DataFrame | read_csv("filename.csv") | df = pd.read_csv("data.csv") |
Specify separator | sep parameter | pd.read_csv("data.csv", sep=";") |
Read file without header | header=None | pd.read_csv("data.csv", header=None) |
Set column as index | index_col parameter | pd.read_csv("data.csv", index_col="RollNo") |
Load only specific columns | usecols parameter | pd.read_csv("data.csv", usecols=["Name","Marks"]) |
Export DataFrame to CSV | to_csv("filename.csv") | df.to_csv("output.csv") |
Export without row index | index=False | df.to_csv("output.csv", index=False) |
Practice Questions with Answers
Q1. What is a CSV file Give an example
Ans. A CSV file is a text file in which data is stored in tabular form. Each line represents a record and values are separated by commas.
Example:
RollNo,Name,Marks
101,Anita,92
102,Rahul,85
Q2. Name the function used to import a CSV file into a Pandas DataFrame
Ans. The function used is read_csv()
Q3. Which parameter of read_csv is used to select only specific columns from the file
Ans. The parameter usecols
is used to select specific columns.
Q4. Write a short program to read a CSV file named marks.csv and display only the columns RollNo and Marks
Ans.
import pandas as pd
df = pd.read_csv("marks.csv", usecols=["RollNo", "Marks"])
print(df)
Q5. How can you save a DataFrame without row numbers into a CSV file
Ans. Use the parameter index=False
with the to_csv function.
Example:
df.to_csv("output.csv", index=False)
Q6. Write a program to read a CSV file students.csv add a new column Grade with values A B and C and then save the updated DataFrame into a new file students_new.csv
Ans.
import pandas as pd
df = pd.read_csv("students.csv")
df["Grade"] = ["A", "B", "C"]
df.to_csv("students_new.csv", index=False)