Page 195 - Robotics and AI class 10
P. 195

To sort the records in the file:

              s1 = s.sort_values(by='Name',ascending = False)
              print(s1.head(5))
               • Accessing the element of the Series
               The elements can be accessed using either positional index or labelled index.
               For example:
               import pandas as pd

              Marks = pd.Series([96,92,91,93],index=["Shashi","Sonali","Neha,"Rajeev"])
              print("Using Labelled index ", Marks["Sonali"])
              print("Using Positional index ", Marks[1])
               Output:

              Using Labelled index 92
              Using Positional index 92
               To print 2 elements :
               print(Marks[[2,3]])
               Output:
               Neha    91

              Rajeev 93
              dtype: int64
               print(Marks[["Sonali","Rajeev"]])
               Output:
               Sonali 92
              Rajeev 93
              dtype: int64
               • Elements can be accessed using Slicing as follows:
               print(Marks[1:3])  #excludes the value at index position 3
               Output:
               Sonali 92

              Neha     91
              dtype: int64
               print(Marks["Sonali":"Rajeev"])  #includes the value at labelled index
               Output:
               Sonali 92

              Neha     91
              Rajeev 93
              dtype: int64
               • To display the series in the reverse order
               print(Marks[::-1])
               Output:
               Rajeev 93



                                                         Introduction to Data and Programming with Python  193
   190   191   192   193   194   195   196   197   198   199   200