CBSE CS and IP

CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Python Pandas - Series Attribute

Python Pandas - Series Attribute


Attributes are the properties of any object. Here we will discuss all the Series attributes with programming examples. All the important Series attributes according to the CBSE Class 12 Informatics practices syllabus are given below in the table:-


Attributes Description
Series.index Range of the index (axis labels) of the Series.
Series.values Return Series as ndarray or ndarray like depending upon dtype
Series.dtype Return the dtype object of the underlying data.
Series.shape Return a tuple of the shape of the underlying data.
Series.nbytes Return the number of bytes in the underlying data.
Series.ndim The number of dimensions of the underlying data, by definition 1.
Series.size Return the number of elements in the underlying data.
Series.hasnans Return if I have any nans; enables various perf speedups.
Series.empty Return true if Series is empty
at, iat To access a single value from Series
loc, iloc To access slices from Series


Let us now check all the attribute with programming example. We will consider the following Series Student and check all the attributes on this Series Student.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import pandas as pd
student = pd.Series(["Sonal", "Rahul", "Mohan", "Siya",])
print(student)

'''
Output:
0    Sonal
1    Rahul
2    Mohan
3     Siya
dtype: object
'''

1. Series.index
This attribute is used to get the range of the index (axis labels) of the Series. Let us try this function on the student Series.
1
2
>>> student.index
RangeIndex(start=0, stop=4, step=1)

2. Series.values
values attribute returns Series as ndarray or ndarray like depending upon dtype.
1
2
>>> student.values
array(['Sonal', 'Rahul', 'Mohan', 'Siya'], dtype=object)

3. Series.dtype
dtype attribute is used to check the data type of the Series Object. Since the student series is of object type, below output is showing 'o'.
1
2
>>> student.dtype
dtype('O')

4. Series.shape
shape attribute gives the shape of the underlying data structure in the form of a tuple. Since the student Series is having 4 elements the output is (4,).
1
2
>>> student.shape
(4,)
5. Series.nbytes
nbyte attribute gives the total number of bytes taken by the Series object to store the data. The below-given output tells that the student object takes 32 bytes of memory.
1
2
>>> student.nbytes
32

6. Series.ndim
ndim gives the dimension of the underlying data structure. Since series is a 1-D data structure, for all series object it gives 1.
1
2
>>> student.ndim
1

7. Series.size
size gives the total number of elements in the series. Since the student series has 4 elements size will give 4.
1
2
>>> student.size
4

8. Series.hasnans
hasnans returns Boolean value. If any of the series elements is NaN it will return True. Otherwise false.
1
2
>>> student.hasnans
False

9. Series.empty
empty attribute returns Boolean True if Series is empty, otherwise the output will be False.
1
2
>>> student.empty
False

10. at, iat
We will discuss at and iat in detail in our upcoming Post. You can click here to go to the post.

11. loc, iloc
We will discuss loc and iloc in detail in our upcoming Post. You can click here to go to the post.




1 comment:

  1. Your website is really cool and this is a great inspiring article. Thank you so much.Serie

    ReplyDelete