Header Ads Widget

Responsive Advertisement

Series Functions and attributes : pandas in python


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
>>> seriesAlph
a 10
b 50
c 500
d 500
e 500
f 15
dtype: int32


Attributes of Series We can access certain properties called attributes of a series by using that property with the series name. There is some important list series attributes. 



There are more extra attribute exam point of view. We will discuss all these attribute.

Methods of Series
In this section, we are going to discuss some of the methods that are available for Pandas Series. Let us consider the following series:
>>> seriesTenTwenty=pd.Series(np.arange( 10, 20, 1 ))
>>> print(seriesTenTwenty)
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
dtype: 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 





What is pandas in Python with example? What are pandas series in Python? What is NumPy and pandas in Python? pandas in python install pandas in python tutorial pandas in python example pandas in python w3schools pandas in python methods pandas in python geeksforgeeks pandas in python interview questions pandas in python javatpoint How do I install pandas in Python? Can I pip install pandas? How do I download pandas in Python idle? Is pandas already in Python? install pandas in python windows how to install pandas in python idle pip install pandas python 3 install pandas in pycharm install pandas in jupyter notebook how to import pandas in python conda install pandas pip install pandas error ncert ip book class 12 python ncert ip book class 12 solutions pdf ncert books class 12 ncert ip book class 11 solutions pdf informatics practices class 11 book pdf informatics practices class 12 ncert textbook pdf download 2020-21

Post a Comment

0 Comments