0% found this document useful (0 votes)
2 views14 pages

Introduction to Pandas Data Structures

This document provides an introduction to the two primary data structures in pandas: Series and DataFrame. It explains how to create, manipulate, and perform operations on these structures, including selecting elements, assigning values, and applying functions. Additionally, it covers advanced functionalities such as reindexing, dropping elements, and arithmetic operations between different data structures.

Uploaded by

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

Introduction to Pandas Data Structures

This document provides an introduction to the two primary data structures in pandas: Series and DataFrame. It explains how to create, manipulate, and perform operations on these structures, including selecting elements, assigning values, and applying functions. Additionally, it covers advanced functionalities such as reindexing, dropping elements, and arithmetic operations between different data structures.

Uploaded by

kohilavictor.r
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Module 4 -Part II

Introduction to pandas Data Structures

The heart of pandas is just the two primary data structures on which all
transactions, which are generally made during the analysis of data, are
centralized:

• Series

• DataFrame

1. The Series
The Series is the object of the pandas library designed to represent one-
dimensional data structures, similarly to an array but with some additional
features. The main array has the purpose to hold the data (data of any
NumPy type) to which each element is associated with a label, contained
within the other array, called the Index.

i) Declaring a Series

To create the Series as specified simply call the Series( ) constructor


passing as an argument an array containing the values to be included in
it.

>>> s = [Link]([12,-4,7,9])

>>> s

0 12 1 -4 2 7 3 9

dtype: int64

it will be necessary, during the constructor call, to include the index


option assigning an array of strings containing the labels.

s = [Link]([12,-4,7,9],

index=['a','b','c','d'])

>>> s

a 12

b -4

c7

d9
dtype: int64

ii) Selecting the Internal Element

>>> s[2]

Or you can specify the label corresponding to the position of the index.

>>> s['b']

-4

In the same way you select multiple items in a numpy array, you can
specify the following:

>>> s[0:2]

a 12

b -4

dtype: int64

iii) Assigning Values to the Element


iv) Defining Series from NumPy Arrays and Other Series
v) Operations and Mathematical Function
vi) Evaluating Values
vii) Nan values
viii) Series as dictionaries
ix) Operations between Series

Explain the above subheadings with example..refer


notes/text book

[Link] DataFrame
The DataFrame is a tabular data structure very similar to the
Spreadsheet (the most familiar are Excel spreadsheets). This data
structure is designed to extend the case of the Series to multiple
[Link] DataFrame consists of an ordered collection of columns
each of which can contain a value of different type (numeric, string,
Boolean, etc.)
1.
Defining a DataFrame
>>> data = {'color' : ['blue','green','yellow','red','white'], 'object' :
['ball','pen','pencil','paper','mug'], 'price' : [1.2,1.0,0.6,0.9,1.7]}

frame = [Link](data)

>>> frame

you can specify a sequence of columns, using the columns option. The
columns will be created in the order of the sequence regardless of how
they are contained within the object dict.
, you have to use the index option assigning it an array containing the
labels

Using reshape(),

i) Selecting Elements
ii) Assigning Value

For example, within the DataFrame structure an array of indexes


is specified by the index attribute, and the row containing the
name of the columns is specified with the columns attribute,you
can also assign a label, using the name attribute, to these two
substructures for identifying them.

iii) Deleting a Column

iv) Transposition of a DataFrame


Other Functionalities on Indexes
 Reindexing
• Dropping
• Alignment
1. Reindexing

Once declared within a data structure, the Index object cannot be changed. This is
true, but by executing a reindexing you can also overcome this problem.

[Link]
pandas provides a specific function for this operation: drop( ). This method will return a new
object without the items that you want to delete.
If want to remove a single item from a Series. To do this, define generic Series 4 elements
with four distinct labels.
Operations between Data Structures
Arithmetic Alignment
The same operations can also be performed using appropriate methods, called Flexible
arithmetic methods.

pandas allows you to make transactions even between different structures as for example, a
DataFrame and a Series. For example, we define these two structures in the following way.

The two newly defined data structures have been created specifically so that the indexes of
Series match with the names of the columns of the DataFrame.
The elements of the series are subtracted from the values of the data frame corresponding to
the same index on the column.
The value is subtracted for all values of the column, regardless of their index. If an index is
not present in one of the two data structures, the result will be a new column with that index
only that all its elements will be NaN.

Function Application and Mapping


[Link] by Element
The pandas library is built on the foundations of [Link]
these are the universal functions, called ufunc. This class of functions
operates by element in the data structure.
Example:
[Link] by row or column
Using the apply( ) function you can apply the function just defined on the DataFrame.

It is not mandatory that the method apply( ) returns a scalar value. It can also return a
Series.
we will have two or more values for each feature applied. This can be done by defining a
function in the following manner:
2. Statistics function

functions such as sum() and mean() can calculate the sum and
the average, respectively, of the elements contained within a
dataframe.

Example :
3. Sorting and ranking

Pandas provides the sort_index() function, which returns a new


object that’s identical to the start, but in which the elements are
ordered.
Example 1:
Example 2:

If you want to order the series, you need to use the sort_values() function.
[Link]()
[Link]()

[Link]()-
The ranking is an operation closely related to sorting. It mainly
consists of assigning a rank (that is, a value that starts at 0 and then
increase gradually) to each element of the series. The rank will be
assigned starting from the lowest value to the highest value
Example:
The rank can also be assigned in the order in which the
data are already in the data structure (without a sorting
operation). In this case, just add the method option with
the ‘first’ value assigned.

[Link]()

You might also like