Sonzaschool
Rudi

Sekondari ya Kawaida · Kidato cha Nne · Amali · Electronics and Computer

Computer Programming

Implementing local data storage

takriban dakika 4 kusoma

Majadiliano
Mada za sehemu hiiCollecting and storing dataMada 4

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

Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa

Tathmini ya mchakato

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

Tathmini ya matokeo

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

Tathmini ya maarifa

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.

Swali

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

Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.

Ingia ili kufanya mazoezi

Mwalimu

Umekwama? Niulize chochote kuhusu mada hii.

Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.

Ingia ili kuuliza

Majadiliano

Hakuna maswali bado

Ingia ili kuuliza
Inapakia majadiliano…