KEMBAR78
PTTQHDL - Lab 01 | PDF | Computer Programming | Computer Engineering
0% found this document useful (0 votes)
5 views11 pages

PTTQHDL - Lab 01

The document outlines various data structures in Python, including lists, dictionaries, series, and data frames, with practical examples for each. It explains how to create, access, update, and delete elements within these structures, as well as their unique properties and methods. Additionally, the document includes exercises to reinforce understanding of these concepts.

Uploaded by

hangocnhi0006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

PTTQHDL - Lab 01

The document outlines various data structures in Python, including lists, dictionaries, series, and data frames, with practical examples for each. It explains how to create, access, update, and delete elements within these structures, as well as their unique properties and methods. Additionally, the document includes exercises to reinforce understanding of these concepts.

Uploaded by

hangocnhi0006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab 01 – Data Structures

This lab will demonstrate these various structures in detail with practical examples: List,
dictionaries, tuples, series, data frames and panels.

List
A list in Python is an ordered collection of elements that can be of any data type. Each
element, also known as a list item, is indexed sequentially, starting from 0. Lists are
mutable, meaning individual elements can be modified without creating a new list. They
support various operations such as adding, sorting, and removing elements. Lists are
defined using square brackets ([]), with items separated by commas, and they maintain the
order in which they are created.

Crea&ng a List
Accessing Values in Lists

List elements can be accessed in both forward and reverse order. For example,
using slicing, list2[3:] retrieves all elements starting from index 3 to the end of the
list. In the case of list2 containing four elements, the value at index 3 is [4,5], which
is a nested list. As a result, the output will be [[4,5]].

Basic List Opera&ons


Similar to string operations, lists support the + and * operators for concatenation
and repetition. The + operator combines two lists into a new one, while
the * operator repeats the elements of a list a speci@ied number of times, producing
a new list as the result.

Adding and Upda&ng Lists


Lists allow updating of single or multiple elements by assigning new values to a
speci@ied slice. This is done by placing the slice on the left side of the assignment
operator. Additionally, new elements can be added to the list using
the append() method, which inserts an item at the end of the list.
Dele&ng Elements
To remove an element from a list, you can use the del statement with the element's
index or the remove() method with the element's value. When using remove(), only
the @irst occurrence of the speci@ied value is deleted if it appears multiple times.
Additionally, the pop() method can be used to remove an element at a speci@ic
index, returning the removed value.
Methods of List

Dictionaries
A dictionary is an unordered collection of key-value pairs, where each key is
separated from its corresponding value by a colon (:), and pairs are separated by
commas within curly braces ({}). An empty dictionary is represented by {}.
Dictionary keys must be unique and of an immutable data type, such as a string or
an integer, while values can be of any data type and may appear multiple times.
Dictionaries serve as a mapping between keys and values and can also be created
using the dict() function.
Crea&ng Dic&onaries
A dictionary can be created by directly assigning key-value pairs within curly
braces {}. Alternatively, an empty dictionary can be initialized @irst, and key-value
pairs can be added later by assigning values to speci@ic keys.

Upda&ng and Accessing Values in Dic&onaries

Dele&ng Dic&onary Elements


Individual elements in a dictionary can be removed by specifying their key, or the
entire dictionary can be cleared using the clear() method. Additionally,
the del keyword can be used to delete speci@ic key-value pairs or remove the
dictionary entirely in a single operation.
Built-in Dic&onary Methods

Series
A series is essentially a one-dimensional array with labels, capable of storing various data
types such as integers, strings, floating-point numbers, Python objects, and more.
For example:
SeriesX = pd.Series(data, index=index)
In this context, pd refers to the Pandas library, while data can be a Python dictionary, a
NumPy ndarray, or even a single scalar value.

Crea&ng a Series with index


When the data is an ndarray, the index is provided as a list of labels for the axes. If
no index is supplied, Python automatically generates an index starting from 0 and
incrementing up to n-1, where n is the length of the data.
Creating a Series of Ndarray Data Without Labels

Creating a Series of Ndarray Data with Labels

Slicing Data from a Series

Creating a Series from a Dictionary


Vectorized Opera&ons and Label Alignment with Series
Operations on Series are performed with automatic alignment based on the labels.
This means you can perform calculations without worrying about whether the
Series have matching labels. If the labels do not align, the result will contain a
missing value represented by NaN.

Data Frames
A DataFrame is a two-dimensional, labeled data structure that can store columns with
potentially different data types. It can be created from various data sources, including:
• A 1D ndarray, list, dictionary, or Series
• A 2D NumPy ndarray
• A structured or record ndarray
• Another DataFrame
A DataFrame also has two key components: an index (for row labels) and columns (for
column labels).

Crea&ng Data Frames


Creating a Data Frame from a Dict of Series
Creating a Data Frame from an Ndarray

Creating a Data Frame from a List of Dictionaries


data2 = [{'A': 1, 'B': 2}, {'A': 5, 'B': 10, 'C': 20}]
pd.DataFrame(data2)
Adding Columns and Making Operations on a Created Data Frame

Indexing and Selec&ng Data Frames


Exercises
1. For the given code arr = np.array([6, 3, 23, 12, 9, 9, 5, 7, 12, 17, 67,

53]), write a single line of code to replace the even elements with 0.
2. Give two arrays:
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.array([7, 8, 9, 10, 11, 12])
How can you remove elements from one array that are present in another?
3. Give an array:
arr = np.arange(9).reshape(3,3)

Write a line of code that creates a new array from the arr array, and swaps the first
and last columns.
4. Write a program that reads data from a text file and generates a dictionary of all the
keywords in the file. The program should count the frequency of each word and
identify the word with the highest repetition. Display both the dictionary of words
and the most frequently occurring word.
5. Study and present the functions in Pandas that handle sorting an operations.
6. Study and present the functions in Pandas that handle ranking operations.

You might also like