Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Series

Consider the given data set of students:

Name Shruti Renu Rinku Nidhi Suraj Rohit Vidooshi


Marks 80 60 89 78 99 92 69

1. Create a Series from the above data, Index as Name and Marks as values.
Ans : Using List :
import pandas as pd
Name=["Shruti" , "Renu" , "Rinku" , "Nidhi" , "Suraj" , "Rohit" , "Vidooshi"]
Marks=[80,60,89,78,99,92,69]
stud=pd.Series(Marks,index=Name)
print(stud)
Using Dictionary :
import pandas as pd
dict = {"Shruti" : 80 , "Renu" :60 , "Rinku" :89 , "Nidhi" :78 ,
"Suraj" :99 , "Rohit" : 92 , "Vidooshi" : 69}
stud=pd.Series(dict)
print(stud)

Using Numpy-ndarray :
import pandas as pd
import numpy as np
Name=["Shruti" , "Renu" , "Rinku" , "Nidhi" , "Suraj" , "Rohit" , "Vidooshi"]
Marks=[80,60,89,78,99,92,np.NaN]
arr=np.array(Marks)
stud=pd.Series(arr,index=Name)
print(stud)

2. Adding new values in a list:


Ans : stud["Prakash"]=66
print(stud)

3. Deleting element from a series :


Ans : stud=stud.drop("Vidooshi")
print(stud)
4. Deleting multiple elements from a series :
Ans : stud=stud.drop(["Vidooshi","Renu"])
print(stud)
5. Display elements of the series :
Ans: print(stud["Suraj"]) OR print(stud[4])

6. Display multiple elements of the series :


Ans: print(stud["Suraj"] , stud["Nidhi") OR print(stud[4] , stud[3])

7. Display the records of those students who have scored above 85.
Ans : print(stud[stud>85])
Rinku 89
Suraj 99
Rohit 92
8. Display the records of those students who have scored less than equal to 80.
Ans : print(stud[stud<=80])
Shruti 80
Nidhi 78
Prakash 66

9. Deleting NaN values :


Ans : stud=stud.dropna()
print(stud)

10. Deleting Duplicate values :


Ans : stud=stud.drop_duplicates()
print(stud) #deletes only the second repeating element first remains.

Write the output of the following based on the above Series :


Shruti 80
Renu 60
Rinku 89
Nidhi 78
Suraj 99
Rohit 92
Vidooshi 69
1. print(stud[1:4]) #This is slicing : Start=1 , End = 4-1=3 index
Ans : Renu 60
Rinku 89
Nidhi 78
2. print(stud["Nidhi":"Rohit"]) #Slicing using row labels
Ans : Nidhi 78
Suraj 99
Rohit 92
#There we are using row labels of elements so end-1 is not applicable.
3. print(stud[:3]) #This is slicing : Start=0 , End = 3-1=2 index
Ans : Shruti 80
Renu 60
Rinku 89
4. print(stud[3:]) # Staring from 3 index to last
Ans : Nidhi 78
Suraj 99
Rohit 92
Vidooshi 69
5. print(stud[0:5:2]) #Here : Start=0 , End = 5-1=4 , step=2
Ans : Shruti 80
Rinku 89
Suraj 99
6. print(stud.head()) #head function without parameter show first 5 values.
Ans : Shruti 80
Renu 60
Rinku 89
Nidhi 78
Suraj 99
7. print(stud.tail()) #tail function without parameter show last 5 values.
Ans : Rinku 89
Nidhi 78
Suraj 99
Rohit 92
Vidooshi 69
8. print(stud.head(2)) #head function with parameter shows first 2 values.
Ans : Shruti 80
Renu 60

9. print(stud.tail(4)) #tail function with parameter show last 4 values.


Ans : Nidhi 78
Suraj 99
Rohit 92
Vidooshi 69

10. print(stud.loc["Renu" : "Suraj"]) #Same as Slicing using row labels


Ans : Renu 60
Rinku 89
Nidhi 78
Suraj 99

11. print(stud.iloc[1:4]) #Same as slicing : Start=1 , End = 4-1=3 index


Ans : Renu 60
Rinku 89
Nidhi 78

Series Attributes
1. print(stud.index)
Ans : Index(['Shruti', 'Renu', 'Rinku', 'Nidhi', 'Suraj', 'Rohit', 'Vidooshi']
2. print(stud.values)
Ans : [80 60 89 78 99 92 69]
3. print(stud.size)
Ans : 7
4. print(stud.hasnans)
Ans : False
5. print(stud.empty)
Ans : False
6. print(stud.count())
Ans : 7

You might also like