Python Tutorial: Pandas Series Exercises

Friday, 28 April 2023

Pandas Series Exercises

Q1.         Consider a given Series ,

Series1:    200    700

           201    700

           202    700

           203    700

           204    700

Write a program in Python Pandas to create the series and display it.

Solutions

import pandas as pd

Series1=pd.Series(700,index=range(200,205))

print(Series1)

OUTPUT

Q2. Consider the following Series object, s

                        IP                    95

Physics          89

Chemistry     92

Math              95

i.  Write the Python syntax which will display only IP.

ii.  Write the Python syntax to increase marks of all subjects by 10.

Solution:

import pandas as pd

s=pd.Series([95,89,92,95],index=['IP','Physics','Chemistry','Math'])

print(s)

print(s.index[0])

print('='*10)

s=s+10

print(s)

Syntax:

seriesObject.index[index_number]

seriesObject=seriesObject+10

OUTPUT:

Q3. Given the following Series1

  

Q4. Write the command to create above Series and then double the value in series and store in another series named Series2

Solution:

import pandas as pd

Series1=pd.Series([100,200,300,400,500],index=['A','B','C','D','E'])

Series2=Series1*2

print(Series1)

print(Series2)

OUTPUT:
A    100
B    200
C    300
D    400
E    500
dtype: int64
A     200
B     400
C     600
D     800
E    1000
dtype: int64

Q5.Consider a given series : SQTR

QTR1       50000

QTR2       65890

QTR3       56780

QTR4       89000

QTR5       77900

Write a program in Python Pandas to create and display the series.

Solution:

import pandas as pd

val1=[50000,65890,56780,89000,77900]

idx=['QTR1','QTR2','QTR3','QTR4','QTR5']

SQTR=pd.Series(val1,index=idx)

print(SQTR)

OUTPUT:

QTR1    50000

QTR2    65890

QTR3    56780

QTR4    89000

QTR5    77900

dtype: int64

Q6. Creating a Series a Company gets same profit Rs. 80000 in the first three months of the year 2022. Specify index with JAN, FEB, MAR.

Solution

import pandas as pd

n=80000

S=pd.Series(n,index=['JAN','FEB','MAR'])

print(S)

OUTPUT:

JAN    80000

FEB    80000

MAR    80000

dtype: int64

Q7. Creating a Series using scalar value ‘CBSE EXAM-2024’ and indices as a with Term-1, Term-2, Term-3.

Solution

import pandas as pd

n='CBSE Exam-2024'

S=pd.Series(n,index=['Term-1', 'Term-2', 'Term-3'])

print(S)

OUTPUT:

Term-1    CBSE Exam-2024

Term-2    CBSE Exam-2024

Term-3    CBSE Exam-2024

dtype: object

Q8. Predict the output in the following Python codes:

1. import pandas as pd

    ob = pd.Series([200, 400, 600, 800, 1000]

    ob[1] = 300

    ob [3:]=400

    print("Series elements are:\n")

    print(ob)

OUTPUT:

Series elements are:

0    200

1    300

2    600

3    400

4    400

dtype: int64

2. import pandas as pd

    Nos1 = [45, 34, 47]

    Nos2 = [32,40,37]

   Sec = [A', 'B', 'C’]

   ob1 = pd.Series( data = Nos * 1 , index = Sec)

   ob2= pd.Series(data = Nos2, index = Sec)

   print(ob1 + ob2)

OUTPUT:

A    77

B    74

C    84

dtype: int64

3. import pandas as pd

Val=[2, 4, 68]

S1 = pd.Series (data = Val)

print(S1 <6)

print(S1[S1<6])

OUTPUT:

0     True

1     True

2    False

dtype: bool

0    2

1    4

dtype: int64

4. import pandas as pd

    S1 = pd.Series ([ 12, 18, 51 ,45,191])

    S2 = pd.Series ([5,8,7,9,2],[1, 2, 4,8, 12])

    print(S1 + S2)

OUTPUT:

0       NaN

1      23.0

2      59.0

3       NaN

4     198.0

8       NaN

12      NaN

dtype: float64

5. import pandas as pd

     L1 = [12, 23, 34, 56,78]

     L2 = [1, 2, 3, 2, 4]

     S1=pd.Series(data=L1, index =L2)

     print(S1[2])

OUTPUT:

2    23

2    56

dtype: int64

6. import pandas as pd

    id=["AMAN", "SUMIT" , "DIMPLE", "KKISHAN"]

    L1 = [62, 73, 59, 82]

    L2 = [69, 84, 66, 79]

    S1=pd.Series(data =L1, index = id)

    S2=pd.Series(data =L2,index=id)

    print (S1+S2)

    S =S1+S2

    print(S)

OUTPUT:

AMAN       131

SUMIT      157

DIMPLE     125

KKISHAN    161

dtype: int64

AMAN       131

SUMIT      157

DIMPLE     125

KKISHAN    161

dtype: int64

7. import pandas as pd

    S1 = ([1, 2, 3, 4])

    print (S1*3)

OUTPUT:

[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

8. import pandas as pd

    S1=pd.Series( [2, 4, 6, 8], [1, 2, 3, 4])

    S1[1:2] = 15

    print(S1)

OUTPUT:

1     2

2    15

3     6

4     8

dtype: int64

 

9. import pandas as pd

    L1 = [12, 23, 34, 56, 18, 29, 62, 34]

    S1=pd.Series( data =L1)

    print( S1[S1 > 40])

OUTPUT:

3    56

6    62

dtype: int64

10. import pandas as pd

       Ist = [6, 3, 5]

       s1 =pd.Series(data = Ist)

       print (Ist* 2)

       print (s1 * 2)

OUTPUT:

[6, 3, 5, 6, 3, 5]

0    12

1     6

2    10

dtype: int64

Q9. Find the errors in the following statements (assuming that the Pandas libra already imported with alias 'pd'):

1. ind = [1, 2, 3, 4, 5]

    S1 = pd.Series([8, 10, 12, 14], index = ind)

Ans:

ind = [1, 2, 3, 4, 5]

S1 = pd.Series([8, 10, 12, 14, 15], index = ind) #does not match length of index

2. S1=pd. Series(1, 2, 3, 4, index = range(4))

Ans:

S1=pd.Series([1, 2, 3, 4], index = range(4))  # type error

3. S1=pd. Series([1, 2, 3, 4], index = range(5))

Ans:

S1=pd. Series([1, 2, 3, 4], index = range(4))

4. S1=pd. Series([1, 2, 3, 4], [1, 2, 3, 4, 5, 6]]

Ans:

S1=pd. Series([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])  #does not match length of index

Q10. Given two series S1 and S2

S1                                                       S2

A        39                                            A     10

B        41                                             B     10

C       42                                             D     10

D        44                                             F     10

Find the output for following python pandas statements?

a. S1[ : 2]*100

b.  S1 * S2

c. S2[ : : -1]*10

Solution:

import pandas as pd

S1=pd.Series([39,41,42,44],['A','B','C','D'])

S2=pd.Series(10,['A','B','D','F'])

print(S1)

print(S2)

print(S1[ : 2]*100)

print(S1 * S2)

print(S2[ : : -1]*10)

OUTPUT:

A    39

B    41

C    42

D    44

dtype: int64

A    10

B    10

D    10

F    10

dtype: int64

A    3900

B    4100

dtype: int64

A    390.0

B    410.0

C      NaN

D    440.0

F      NaN

dtype: float64

F    100

D    100

B    100

A    100

dtype: int64

Q11. Given the following Series S1 and S2:

S1                    S2

A                10         A      5

B                20         B       4

C               30         C      6

D                40         D      8

Write the command to find the multiplication of series S1 and S2

SOLUTION:

import pandas as pd

S1=pd.Series([10, 20, 30, 40],['A','B','C','D'])

S2=pd.Series([5, 4, 6, 8],['A','B','C','D'])

print(S1*S2)

OUTPUT:

import pandas as pd

S1=pd.Series([10, 20, 30, 40],['A','B','C','D'])

S2=pd.Series([5, 4, 6, 8],['A','B','C','D'])

print(S1*S2)

Q12. Consider the following Series object, “E- bike company” and its profit in Crores

Vespa              350

Komaki          200

Ather              800

Ola                  150

i. Write the command which will display the name of the company having profit>250.

ii. Write the command to name the series as Profit.

Solution:

import pandas as pd

profit=[350,200,800,150]

idx=['Vespa','Komaki','Ather','Ola']

company=pd.Series(profit,index=idx)

print(company[company>250])

company.name="Profit"

print(company)

OUTPUT:

Vespa    350

Ather    800

dtype: int64

Vespa     350

Komaki    200

Ather     800

Ola       150

Name: Profit, dtype: int64

Q13. Write a Python code to create a Series S1 by using indices ranging from 10 to 20 at an interval of 2 and data ranging from 100 to 500 at an interval of 100. Display the Series. 

SOLUTION:

import pandas as pd

S1=pd.Series(range(100,501,100),index=range(10,20,2))

print(S1)

OUTPUT

10    100

12    200

14    300

16    400

18    500

dtype: int64

Q13. Write a Python code to input five indices and data in a list and tuple respectively. Create a Series using object of your  choice with the given indices and data. Finally, display Series.

import pandas as pd

data=eval(input("Enter your Data in list "))

S1=pd.Series(data)

print(S1)

data2=eval(input("Enter your Data in tuple "))

S2=pd.Series(data2)

print(S2)

OUTPUT:

Enter your Data in list [10,30,60,70,90]

0    10

1    30

2    60

3    70

4    90

dtype: int64

Enter your Data in tuple (10,20,30,40,50)

0    10

1    20

2    30

3    40

4    50

dtype: int64

Q14. Write a Python code to create Series (Ser1) by using the following specifications:

indices : 7, 4, 9, 2, 5

Data: 45, 67, 89,52,38

Organized the data based on:
(i) Ascending order of indices
(ii) Descending order of data
Finally, display the series after sorting in specified orders. 

 SOLUTION:

import pandas as pd

indices =[ 7, 4, 9, 2, 5]

Data=[ 45, 67, 89, 52, 38]

S1=pd.Series(Data,indices)

print("Original Series :")

print(S1)

print("Ascending Order of indices :")

print(S1.sort_index())

print("Descending order of data :")

print(S1.sort_values(ascending=False))

OUTPUT:

Original Series :

7    45

4    67

9    89

2    52

5    38

dtype: int64

Ascending Order of indices :

2    52

4    67

5    38

7    45

9    89

dtype: int64

Descending order of data :

9    89

4    67

2    52

7    45

5    38

dtype: int64

Q14. The name and ages of five students of your school are as given below:

Names : Satyansh, Vedant, Harman, Ayushi, Gargi

Ages: 17,16,17,18, 19

  Write a Python code to create a Series using names and ages as indices and data respectively. Sort the information based on alphabetical order of names. Finally, display the Series.

SOLUTION:

import pandas as pd

Names = ["Satyansh", "Vedant", "Harman", "Ayushi", "Gargi"]

Ages = [17,16,17,18,19]

S1=pd.Series(Ages,Names)

print("Original Series :")

print(S1)

print("Ascending Order of indices :")

print(S1.sort_index())

OUTPUT:

Original Series :

Satyansh    17

Vedant      16

Harman      17

Ayushi      18

Gargi       19

dtype: int64

Ascending Order of indices :

Ayushi      18

Gargi       19

Harman      17

Satyansh    17

Vedant      16

dtype: int64

Q15. Write a Python code to create a Series using item number and price if four items as indices and data correspondingly. The price of items has increased by 5%. Update the price of each item and display the Series.

SOLUTION:

import pandas as pd

price = eval(input("Enter price list of four items :"))

S1=pd.Series(price,index=range(4))

print("Original Series :")

print(S1)

S1=S1+S1*5/100

print("Update 5% increase price list :")

print(S1)

OUTPUT:

Enter price list of four items :[2300,4000,2000,1500]

Original Series :

0    2300

1    4000

2    2000

3    1500

dtype: int64

Update 5% increase price list :

0    2415.0

1    4200.0

2    2100.0

3    1575.0

dtype: float64

Q16. Write a Python code to accept the names of ten cricket players and their country names in two 1D lists correspondingly. Display the player’s name along with the country who belongs to “India”

SOLUTION:

import pandas as pd

players = eval(input("Enter 10 players list :"))

cont = eval(input("Enter Country name :"))

S1=pd.Series(cont,index=players)

print("Original Series :")

print(S1)

print("Show India Cricket Players")

print(S1[S1=='India'])

OUTPUT:

Enter 10 players list :['Virat','Moeen','Dhoni','Michael','Steve','Rishabh','George','Sachin','Nathan','Aiden']

Enter Country name :['India','England','India','England','India','Australia','India','Australia','Africa','Africa']

Original Series :

Virat          India

Moeen        England

Dhoni          India

Michael      England

Steve          India

Rishabh    Australia

George         India

Sachin     Australia

Nathan        Africa

Aiden         Africa

dtype: object

Show India Cricket Players

Virat     India

Dhoni     India

Steve     India

George    India

dtype: object

Q17. Write a Python code create two Series objects with following specifications:

S1

S2

Indices

Data

Indices

Data

0

12

0

15.67

1

38

1

34.42

2

42

2

18.91

3

56

3

25.46

4

31

4

39.99

 Perform the following operations:

(i)                 Add data of both the Series objects correspondingly.

(ii)               Multiply the data of both the Series objects correspondingly.

(iii)            Enlist the data which are correspondingly bigger in both the Series objects.

(iv)             Enlist the data which are correspondingly smaller in both the Series objects.

Display the information after each operation.

SOLUTION:

import pandas as pd

Data1=[12,38,42,56,31]

Data2=[15.67,34.42,18.91,25.46,39.99]

S1=pd.Series(Data1)

S2=pd.Series(Data2)

print("First Series")

print(S1)

print("Second Series")

print(S2)

print("Add both Series")

print(S1+S2)

print("Larger value :")

print(max(S1), max(S2), sep=',')

print("Smaller value :")

print(min(S1), min(S2), sep=',')

OUTPUT:

First Series

0    12

1    38

2    42

3    56

4    31

dtype: int64

Second Series

0    15.67

1    34.42

2    18.91

3    25.46

4    39.99

dtype: float64

Add both Series

0    27.67

1    72.42

2    60.91

3    81.46

4    70.99

dtype: float64

Larger value :

56,39.99

Smaller value :

12,15.67

Q18. Write a python code to create a Series with the following specifications:

Indices : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Data: 17, 16, 18, 15, 24, 65, 44, 87, 27, 69

Display the data corresponding to the indices:

(i)                 From 1 to 5

(ii)               From 0 to 9 at interval of 3

(iii)            From 6 to last

(iv)             From Ist 4 indices

SOLUTION:

import pandas as pd

Indices =[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Data =[ 17, 16, 18, 15, 24, 65, 44, 87, 27, 69]

S1= pd.Series(Data, Indices)

print("Original Series")

print(S1)

print("Show Series From 1 to 5")

print(S1.loc[1:5])

print("Show Series From 0 to 9 at interval of 3")

print(S1.loc[0:9:3])

print("Show Series From 6 to last")

print(S1.tail(6))

print("Show Series From Ist 4 indices")

print(S1.loc[1:4])

OUTPUT:

Original Series

0    17

1    16

2    18

3    15

4    24

5    65

6    44

7    87

8    27

9    69

dtype: int64

Show Series From 1 to 5

1    16

2    18

3    15

4    24

5    65

dtype: int64

Show Series From 0 to 9 at interval of 3

0    17

3    15

6    44

9    69

dtype: int64

Show Series From 6 to last

4    24

5    65

6    44

7    87

8    27

9    69

dtype: int64

Show Series From Ist 4 indices

1    16

2    18

3    15

4    24

dtype: int64

 OR

import pandas as pd

Indices =[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Data =[ 17, 16, 18, 15, 24, 65, 44, 87, 27, 69]

S1= pd.Series(Data, Indices)

print("Original Series")

print(S1)

print("Show Series From 1 to 5")

print(S1[1:6])

print("Show Series From 0 to 9 at interval of 3")

print(S1[0:10:3])

print("Show Series From 6 to last")

print(S1.tail(6))

print("Show Series From Ist 4 indices")

print(S1[1:5])

OUTPUT:

Original Series

0    17

1    16

2    18

3    15

4    24

5    65

6    44

7    87

8    27

9    69

dtype: int64

Show Series From 1 to 5

1    16

2    18

3    15

4    24

5    65

dtype: int64

Show Series From 0 to 9 at interval of 3

0    17

3    15

6    44

9    69

dtype: int64

Show Series From 6 to last

4    24

5    65

6    44

7    87

8    27

9    69

dtype: int64

Show Series From Ist 4 indices

1    16

2    18

3    15

4    24

dtype: int64

Q19. Write a Python code to assign the names and the total marks of 3 subjects, secured by 10 students of class XII in a dictionary as key:value pairs respectively. Create a Series 


No comments:

Post a Comment