Introduction to Python Libraries : Python libraries contain a collection of builtin modules that allow us to perform many actions without writing detailed programs for it. Each library in Python contains a large number of modules that one can import and use. NumPy, Pandas and Matplotlib are three well-established Python libraries for scientific and analytical use. These libraries allow us to manipulate, transform and visualise data easily and efficiently. NumPy, which stands for ‘Numerical Python’, is a library we discussed in class XI. Recall that, it is a package that can be used for numerical data analysis and scientific computing. NumPy uses a multidimensional array object and has functions and tools for working with these arrays. Elements of an array stay together in memory, hence, they can be quickly accessed. In this section we will about how to create series using pandas.
In previous article we discussed about How to create series in python using pandas library. In this article we will discuss about how we can access these series element. There are different methods to access these elements.
Accessing Elements of a Series There are two common ways for accessing the elements of a series: Indexing and Slicing.
(A) Indexing Indexing in Series is similar to that for NumPy arrays, and is used to access elements in a series.
Indexes are of two types: positional index and labelled index. Positional index takes an integer value that corresponds to its position in the series starting from 0, whereas labelled index takes any user-defined label as index.
Following example shows usage of the positional index for accessing a value from a Series.
import pandas as pd
seriesNum = pd.Series([10,20,30])
seriesNum[2]
out Put
30
Here, the value 30 is displayed for the positional index 2
we can use labels as indices Here, the value 3 is displayed for the labelled index Mar.
>>> seriesMnths = pd.Series([2,3,4],index=["Feb ","Mar","Apr"])
>>> seriesMnths["Mar"]
3
In the following example, value NewDelhi is displayed for the labelled index India.
>>> seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
>>> seriesCapCntry['India']
'NewDelhi
We can also access an element of the series using the positional index:
>>> seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
>>> seriesCapCntry[1]
'WashingtonDC'
We can also access multiple element of the series using the positional index:
>>> seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
>>> seriesCapCntry[1,2,3]
'WashingtonDC'
'London'
'Paris'
(B) Slicing:
we may need to extract a part of a series , We use slicing method to extract a part of series. Syntax of slicing [Start:End]. Slicing can be use as follows.
>>>seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
>>> seriesCapCntry[1:3] #excludes the value at index position 3
USA WashingtonDC
UK London
dtype: object
labelled indexes are used for slicing
>>>seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
>>> seriesCapCntry['USA' : 'France']
USA WashingtonDC
UK London
France Paris
dtype: object
We can reverse list we use this syntax
>>>seriesCapCntry = pd.Series(['NewDelhi', 'WashingtonDC', 'London', 'Paris'], index=['India', 'USA', 'UK', 'France'])
>>> seriesCapCntry[ : : -1]
France Paris
UK London
USA WashingtonDC
India NewDelhi
We can also use slicing to modify the values of series elements as shown in the following example:
>>> import numpy as np
>>> seriesAlph = pd.Series(np.arange(10,16,1), index = ['a', 'b', 'c', 'd', 'e', 'f'])
>>> seriesAlph
a 10
b 11
c 12
d 13
e 14
f 15
dtype: int32
>>> seriesAlph[1:3] = 50
>>> seriesAlph
a 10
b 50
c 50
d 13
e 14
f 15
dtype: int32
Observe that updating the values in a series using slicing also excludes the value at the end index position. But, it changes the value at the end index label when slicing is done using labels.
>>> seriesAlph['c':'e'] = 500>>> seriesAlpha 10b 50c 500d 500e 500f 15dtype: int32
>>> seriesTenTwenty=pd.Series(np.arange( 10, 20, 1 ))>>> print(seriesTenTwenty)0 101 112 123 134 145 156 167 178 189 19dtype: int32
1. head(n) : Returns the first n members of the series. If the value for n is not passed, then by default n takes 5 and the first five members are displayed.
>>> seriesTenTwenty.head(2)
0 10
1 11
dtype: int32
>>> seriesTenTwenty.head()
0 10
1 11
2 12
3 13
4 14
dtype: int32
2. count() Returns the number of non-NaN values in the Series
>>> seriesTenTwenty.count()
10
3. tail(n) Returns the last n members of the series. If the value for n is not passed, then by default n takes 5 and the last five members are displayed.
>>> seriesTenTwenty.tail(2)
8 18
9 19
dtype: int32
>>> seriesTenTwenty.tail()
5 15
6 16
7 17
8 18
9 19
dtype: int32
0 Comments