Sonzaschool
Back

O-Level · Form 4 · Vocational · Electronics and Computer

Computer Programming

Implementing local data storage

about 4 minutes to read

Discussions
Topics in this sectionCollecting and storing data4 topics

When we process data in Python, we often need to keep a copy of the results on our computer for later use. This is called local data storage — saving processed data to files on your hard drive or removable storage so you can open and use them again without re-running your script.

This topic shows you how to write data from Python programs into two common flat-file formats: CSV (Comma-Separated Values) and JSON (JavaScript Object Notation). Both formats are plain text files that can be opened in many applications, including spreadsheets and text editors.


A CSV file stores data in rows, with each value separated by a comma. It looks like a simple table and is widely supported.

Using pandas to Write CSV

The pandas library provides a simple function called to_csv() to save a DataFrame to a CSV file.

General syntax:

dataframe.to_csv('filename.csv', index=False)
  • The first argument is the name of the file you want to create.
  • The parameter index=False tells Python not to write the row numbers into the file.

Worked Example

Imagine a small shop in Arusha recorded the sales of three items:

ItemQuantityUnit Price (TSh)
Rice501500
Sugar302000
Flour201800

The following Python script saves this data to a file called sales_data.csv:

import pandas as pd

# Create the data
data = {
    'Item': ['Rice', 'Sugar', 'Flour'],
    'Quantity': [50, 30, 20],
    'Unit Price (TSh)': [1500, 2000, 1800]
}

# Make a DataFrame
df = pd.DataFrame(data)

# Save to CSV
df.to_csv('sales_data.csv', index=False)

print("Data saved to sales_data.csv successfully!")

When you open sales_data.csv in a text editor, it looks like:

Item,Quantity,Unit Price (TSh)
Rice,50,1500
Sugar,30,2000
Flour,20,1800

How you'll be assessedProcess assessment · Outcome assessment · Knowledge assessment

Process assessment

  • Write processed data using appropriate file formats.
  • Proper naming and directory organization for stored files.

Outcome assessment

A well-structured local directory with saved datasets in appropriate formats.

Knowledge assessment

Detailed knowledge of:

Method used: The student should explain how to:

  • save cleaned and formatted files to a local computer.

Principles: The student should explain the principles of

  • file format selection for data analysis and sharing.

Theories: The student should explain:

  • Concepts of file formats.

Circumstantial knowledge:

Detailed knowledge about:

  • Handling large datasets that might exceed memory limits during saving.

Question

What does the parameter index=False do when saving a DataFrame to a CSV file using pandas?

Sign in to submit your answer and have it count toward your mastery.

Sign in to practice

Tutor

Stuck? Ask me anything about this topic.

Sign in to ask the Sonza AI Tutor about this question.

Sign in to ask

Discussions

No questions yet

Sign in to ask
Loading discussions…