Artificial Intelligence

Importing and Exporting Data between CSV Files and DataFrames (CBSE Class 12 Artificial Intelligence)

Class 12 · Artificial Intelligence

Importing and Exporting Data between CSV Files and DataFrames

Data is the foundation of every Artificial Intelligence (AI) and Machine Learning (ML) application. However, data is rarely created directly inside a Python program. Most real-world datasets are stored in files such as CSV, Excel, databases, or cloud storage. Therefore, the first step in any AI project is to import data into Python for analysis and processing.

The Pandas library provides simple and powerful functions to import data from CSV files into a DataFrame and export processed data back to a CSV file. This enables data scientists to clean, analyze, transform, and save datasets efficiently.


Learning Objectives

  • Understand CSV files.
  • Import CSV files into a Pandas DataFrame.
  • Export DataFrames to CSV files.
  • Understand the role of CSV files in AI projects.
  • Perform basic data import and export operations.

What is a CSV File?

CSV stands for Comma-Separated Values. It is a simple text file used to store tabular data in rows and columns. Each line represents one row, and commas separate individual values.

Definition

A CSV file is a text file that stores tabular data, where each row represents a record and each value is separated by a comma.


Example of a CSV File



Name,Class,Marks

Aman,XII,92

Neha,XII,88

Rahul,XII,95

When this file is imported into Pandas, it automatically becomes a DataFrame.


Why are CSV Files Important in Artificial Intelligence?

Most datasets used in Artificial Intelligence are stored in CSV format because they are lightweight, easy to create, easy to share, and supported by almost every programming language and data analysis tool.

  • Easy to create and edit.
  • Supported by Excel, Google Sheets, and databases.
  • Easy to import into Python.
  • Suitable for storing structured datasets.
  • Widely used in Machine Learning projects.

Workflow of Data Import



CSV File

     │

     ▼

Pandas read_csv()

     │

     ▼

DataFrame

     │

     ▼

Data Cleaning

     │

     ▼

Machine Learning


Importing a CSV File into a DataFrame

The read_csv() function is used to import a CSV file into a Pandas DataFrame.

Syntax



import pandas as pd

df = pd.read_csv("filename.csv")


Example 1: Importing a CSV File



import pandas as pd

df = pd.read_csv("studentmarks.csv")

print(df)

Output



     Name    Marks

0    Aman      92

1    Neha      88

2   Rahul      95


Importing CSV Files in Google Colab

Follow these steps to import a CSV file into Google Colab.

  1. Open Google Colab.
  2. Create a new notebook.
  3. Click the Folder icon on the left panel.
  4. Click the Upload button.
  5. Select the CSV file from your computer.
  6. Use pd.read_csv() to load the file.


import pandas as pd

df = pd.read_csv("studentmarks.csv")

print(df)


Importing CSV Files in Python IDE

If you are using a desktop Python IDE such as VS Code, IDLE, or PyCharm, provide the complete file path.



import pandas as pd

df = pd.read_csv("C:/PANDAS/studentmarks.csv")

print(df)


Common Parameters of read_csv()

Parameter Purpose
filepath Name or location of the CSV file.
sep Separator used between values (default is comma).
header Specifies the row containing column names.
index_col Sets a column as the index.

Exporting a DataFrame to a CSV File

After cleaning or processing data, it can be saved back to a CSV file using the to_csv() function.

Syntax



df.to_csv("filename.csv")


Example 2: Exporting a DataFrame



import pandas as pd

data = {

'Name':['Aman','Neha'],

'Marks':[90,95]

}

df = pd.DataFrame(data)

df.to_csv("result.csv")

The file result.csv will be created in the current working directory.


Export Without Index

Sometimes, we do not want row numbers (index) to appear in the CSV file. In such cases, use index=False.



df.to_csv("result.csv", index=False)


Flow of Data Export



Processed DataFrame

        │

        ▼

to_csv()

        │

        ▼

CSV File

        │

        ▼

Excel / Database / Machine Learning


Example Workflow



CSV File

      │

      ▼

Import

      │

      ▼

DataFrame

      │

      ▼

Clean Data

      │

      ▼

Export

      │

      ▼

New CSV File


Difference Between Importing and Exporting

Importing Exporting
Reads data into Python. Saves data from Python.
Uses read_csv(). Uses to_csv().
Creates a DataFrame. Creates a CSV file.
Beginning of analysis. Final step after processing.

Real-Life Applications

  • Importing student records for result analysis.
  • Loading hospital patient data.
  • Analyzing bank transaction records.
  • Importing sales reports.
  • Preparing datasets for Machine Learning.
  • Exporting cleaned datasets for future analysis.

Think Like an AI Engineer

A company receives customer data every day in CSV format. Before predicting customer purchasing behaviour, what should be the first Python function used?

Click to View Answer

Use pd.read_csv() to import the dataset into a Pandas DataFrame before cleaning and analyzing it.


Competency-Based Question

A school stores examination results in an Excel sheet and saves them as a CSV file. Explain how Python can import this data, process it, and save the updated results.


Common Beginner Mistakes

  • Incorrect file name or file path.
  • Forgetting to import the Pandas library.
  • Uploading the CSV file to the wrong location in Google Colab.
  • Misspelling read_csv() or to_csv().
  • Forgetting index=False while exporting.

Quick Revision

  • CSV stands for Comma-Separated Values.
  • pd.read_csv() imports CSV files.
  • to_csv() exports DataFrames.
  • CSV files are widely used in AI and Data Science.
  • DataFrames act as the bridge between raw data and Machine Learning.

Memory Trick

Read → Into Python
Write → Out of Python


Exam Tips

  • Remember the syntax of pd.read_csv() and to_csv().
  • Know the difference between importing and exporting data.
  • Understand the purpose of index=False.
  • Practice importing datasets in both Google Colab and Python IDE.

Summary

  • CSV is the most commonly used file format for storing structured datasets.
  • Pandas provides the read_csv() function to import CSV files into DataFrames.
  • The to_csv() function exports processed data back to CSV format.
  • Importing and exporting data are essential steps in every Artificial Intelligence and Machine Learning project.
  • These techniques help prepare datasets for analysis, visualization, and predictive modelling.