Importat Question Panda Series
Importat Question Panda Series
OR
import pandas as pd
S1 = pd.Series( None)
print(S1)
OUTPUT : Series([ ], dtype: float64)
OUTPUT :
0 2
1 4
2 6
3 8
4 10
dtype: int64
OUTPUT :
0 a
1 e
2 i
3 o
4 u
dtype: object
Q10. Write a program in python to create series of given tuple : A = (11, 22, 33, 44, 55)
Ans.
import pandas as pd
A = (11, 22, 33, 44, 55)
S1 = pd.Series(A)
print(S1)
OUTPUT :
0 11
1 22
2 33
3 44
4 55
dtype: int64
Q11. Write a program in Python to create the pandas series of all the characters in the
name accepted from user.
Ans.
import pandas as pd
A = input("Enter your name : ")
S1 = pd.Series(list(A))
print(S1)
OUTPUT :
Enter your name : amit
0 a
1 m
2 i
3 t
dtype: object
Q12. Write a program in Python to create a Series in Python from the given dictionary. D
= {“Jan” : 31, “Feb” : 28, “Mar” : 31}
Ans.
import pandas as pd
D = {"Jan" : 31, "Feb" : 28, "Mar" : 31}
S1 = pd.Series(D)
print(S1)
OUTPUT :
Jan 31
Feb 28
Mar 31
dtype: int64
Q13. Write a program to create a series from dictionary that stores classes (6,7,8,9,10) as
keys and number of students as values.
Ans.
import pandas as pd
D = {6 : 140, 7 : 125, 8 : 100, 9 : 136, 10 : 200}
S1 = pd.Series(D)
print(S1)
OUTPUT :
6 140
7 125
8 100
9 136
10 200
dtype: int64
Q14. Write the output of the following :
import pandas as pd
S1 = pd.Series(12, index = [4, 6, 8])
print(S1)
Ans. Output is :
4 12
6 12
8 12
dtype: int64
Ans.
s 1
u 4
p 7
e 10
r 13
dtype: int64
Pandas Series Class 12 IP Important Questions
Ans.
My 100
name 110
is 120
Amit 130
Gandhi 140
dtype: int64
Ans.
0 1
1 A
2 21
3 1
4 A
5 21
dtype: object
Ans.
0 1
1 7
2 21
dtype: int32
Ans. 121
Q23. Write the output of the following :
import numpy as num
import pandas as pd
arr=num.array([31,47,121])
S1 = pd.Series(arr)
print(S1[0])
Ans. 31
Ans. M
Ans. My
Q26. Which property of series returns the number of elements in the series?
Ans. Size
Q28. Which property of Series help to check whether a Series is empty or not? Explain
with example.
Ans. empty property : This property returns True if the Series is empty otherwise return False.
import pandas as pd
S1 = pd.Series()
print(S1.empty)
OUTPUT :
True
Q29. Fill in the blanks in the given code :
import pandas as pd
____________ = ____________.Series([1, 2, 3, 4, 5])
print(S1)
Ans.
import pandas as pd
S1 = pd.Series([1, 2, 3, 4, 5])
print(S1)
Ans.
import pandas as pd
S1 = pd.Series([10, 20, 30, 40, 71,50])
print(S1[ 4 ])
Pandas Series Class 12 IP Important Questions
Q31. Complete the code to get the required output :
import ______ as pd
________ = pd.Series([31, 28, 31], index = ["Jan", "Feb", "Mar"] )
print(S1["_______"])
OUTPUT :
28
Ans.
import pandas as pd
S1 = pd.Series([31,28,31], index = ["Jan","Feb","Mar"])
print(S1["Feb"])
Q32. Write the output of the following code :
import pandas as pd
S1 = pd.Series([31, 28, 31, 30, 31], index = ["Jan", "Feb", "Mar", "Apr", "May"])
print("-----------")
print(S1[1:3])
print("-----------")
print(S1[:5])
print("-----------")
print(S1[3:3])
print("-----------")
print(S1["Jan":"May"])
Ans.
-----------
Feb 28
Mar 31
dtype: int64
-----------
Jan 31
Feb 28
Mar 31
Apr 30
May 31
dtype: int64
-----------
Series([ ], dtype: int64)
-----------
Jan 31
Feb 28
Mar 31
Apr 30
May 31
dtype: int64
Ans.
May 31
May 31
dtype: int64
Ans.
Jan 31
Mar 31
May 31
dtype: int64
Q36. Write the output of the following :
import pandas as pd
S1 = pd.Series([31, 28, 31, 30, 31], index = ["Jan", "Feb", "Mar", "Apr", "May"])
print(S1[0 : 2] * 2)
Ans.
Jan 62
Feb 56
dtype: int64
Q37. Write a program to modify the value 5000 to 7000 in the following Series “S1”
A 25000
B 12000
C 8000
D 5000
Ans.
import pandas as pd
S1[3]=7000
print(S1)
Ans.
0 4
1 7
2 9
3 12
dtype: int64
0 4
1 10
2 14
3 20
dtype: int64
0 4
1 25
2 49
3 100
dtype: int64
0 0
1 3
2 5
3 8
dtype: int64
0 False
1 True
2 True
3 True
dtype: bool
Ans.
0 3
1 8
2 12
3 17
dtype: int64
Q42. Write a program to display only those values greater than 200 in the given Series
“S1”
0 300
1 100
2 1200
3 1700
Ans.
import pandas as pd
S1 = pd.Series([300, 100, 1200, 1700])
print(S1[S1>200])
Q43. Write a program to display the following Series “S1” in descending order.
0 300
1 100
2 1200
3 1700
Ans.
import pandas as pd
S1 = pd.Series([300, 100, 1200, 1700])
print(S1.sort_values(ascending=False)) #if given True then Series will arrange in ascending
order
OUTPUT :
3 1700
2 1200
0 300
1 100
dtype: int64
Ans.
a 12.0
b 5.0
c NaN
d NaN
e NaN
f NaN
dtype: float64
Ans.
a 12.0
b 5.0
c 0.0
d 0.0
e 0.0
f 0.0
dtype: float64
Q46. Name the methods used for multiplication and division of two Series in Python.
In series we can define our own labeled index In NumPy Arrays we can not define our
to own labeled
access elements of an array. index to access elements of an array
The elements can be indexed in descending The indexing starts with zero for the first
order also. element and the index is fixed
Q50. ________________ is a Pandas data structure that represent one dimensional array
containing a sequence of values of any data type
Ans. Series
Q51. Write a program to change the index of the following Series “S1” from (0, 1, 2, 3) to
(a, b, c,d).
0 120
1 75
2 85
3 95
Ans.
import pandas as pd
S1 = pd.Series([120, 75, 85 ,95])
print("Initial Index Values are : ")
print(S1)
print("Final Index Values are : ")
S1.index=("a","b","c","d")
print(S1)
OUTPUT :
Ans.
0 1
1 2
2 3
3 4
4 1
5 2
6 3
7 4
dtype: int64
0 2
1 4
2 6
3 8
4 2
5 4
6 6
7 8
dtype: int64
Q53. Consider the following Series object “S1” and write the output of the following
statement :
0 21
1 41
2 62
3 81
4 23
5 45
6 68
7 89
import pandas as pd
L1=[21, 41, 62, 81, 23, 45, 68, 89]
S1 = pd.Series(L1)
print("1. ",S1.index)
print("2. ",S1.values)
print("3. ",S1.shape)
print("4. ",S1.ndim)
print("5. ",S1.size)
print("6. ",S1.nbytes)
print("7. ",S1[0])
print("8. ",S1[2]+S1[0])
print("9. ",S1[5]**2)
print("10. ",S1.empty)
print("11.\n",S1[[1, 5, 6]])
print("12.\n",S1[5 : 7],"\n")
print("13.\n",S1[: : -1])
print("14.\n",S1>60)
print("15.\n",S1[S1>60])
print("16.\n",len(S1))
print("17.\n",S1.count())
print("18.\n",S1.head())
print("19.\n",S1.tail())
print("20.\n",S1[4:5] + S1[4:5])
Ans.
11.
1 41
5 45
6 68
dtype: int64
12.
5 45
6 68
dtype: int64
13.
7 89
6 68
5 45
4 23
3 81
2 62
1 41
0 21
dtype: int64
14.
0 False
1 False
2 True
3 True
4 False
5 False
6 True
7 True
dtype: bool
15.
2 62
3 81
6 68
7 89
dtype: int64
16.
8
17.
8
18.
0 21
1 41
2 62
3 81
4 23
dtype: int64
19.
3 81
4 23
5 45
6 68
7 89
dtype: int64
20.
4 46
dtype: int64
Q54. Consider the following Series object “S1” and “S2” and write the output of the
following code :
S1 S2
02 01
14 12
26 23
38 34
4 10 45
Pandas Series
import pandas as pd
S1=pd.Series([2, 4, 6, 8, 10])
S2=pd.Series([1, 2, 3, 4, 5])
print(S1+S2)
print(S1-S2)
print(S1*S2)
print(S1/S2)
print(S1.mul(2))
print(S1*3)
print(S1+3)
print(S1-3)
print(S1.div(S2))
Ans.
0 3
1 6
2 9
3 12
4 15
dtype: int64
0 1
1 2
2 3
3 4
4 5
dtype: int64
0 2
1 8
2 18
3 32
4 50
dtype: int64
0 2.0
1 2.0
2 2.0
3 2.0
4 2.0
dtype: float64
0 4
1 8
2 12
3 16
4 20
dtype: int64
0 6
1 12
2 18
3 24
4 30
dtype: int64
0 5
1 7
2 9
3 11
4 13
dtype: int64
0 -1
1 1
2 3
3 5
4 7
dtype: int64
0 2.0
1 2.0
2 2.0
3 2.0
4 2.0
dtype: float64
import pandas as pd
S1=pd.Series([2, 4, 6, 8, 10])
S2=pd.Series([1, 2, 3, 4, 5])
print(S1+S2) #Addition
print(S1-S2) #Subtraction
print(S1*S2) #Multiplication
print(S1/S2) #Division
Q56. Write a program to create series from the given dictionary. D={ “A” : “Apple”, “B” :
“Boy”, “C” : “Cat”}
Ans.
import pandas as pd
D={ "A" : "Apple", "B" : "Boy", "C" : "Cat"}
S1 = pd.Series(D)
print(S1)
OUTPUT :
A Apple
B Boy
C Cat
dtype: object
Q57. Write a program to create Pandas Series from the given NumPy array.
A=[1,2,3,4,5,6,7]
Ans.
import pandas as pd
import numpy as np
A = np.array([1,2,3,4,5,6,7])
S1 = pd.Series(A)
print(S1)
OUTPUT :
0 1
1 2
2 3
3 4
4 5
5 6
6 7
dtype: int32
Q58. Write a program to display only first n rows from the given Pandas Series ‘S1’.
Accept n from the user.
0 1
1 2
2 3
3 4
4 5
5 6
6 7
Ans.
import pandas as pd
nt = int(input("Enter number of terms to display"))
S1 = pd.Series([1, 2, 3, 4, 5, 6, 7])
print(S1.head(nt))
OUTPUT :
Q59. Write a program to display values greater than 250 from the given Pandas Series
“S1”
0 150
1 252
2 35
3 420
4 50
5 61
6 275
Ans.
import pandas as pd
S1 = pd.Series([150, 252, 35, 420, 61, 275])
print(S1[S1>250])
OUTPUT :
1 252
3 420
5 275
dtype: int64
Q60. Write a program to count the number of values less than 200 from the given Pandas
Series.
0 150
1 252
2 35
3 420
4 50
5 61
6 275
Ans.
import pandas as pd
S1 = pd.Series([150, 252, 35, 420, 61, 275])
print(S1[S1<200].count( ))
OUTPUT :
3
Q61. Write a program to increase those value in the given Pandas Series by 50 which are
less than 70.
0 150
1 252
2 35
3 420
4 50
5 61
6 275
Ans.
import pandas as pd
S1 = pd.Series([150, 252, 35, 420, 61, 275])
S1[S1<70] = S1[S1<70] + 50
print(S1)
OUTPUT :
0 150
1 252
2 85
3 420
4 111
5 275
dtype: int64
Q62. Write a program to create a Series whose values are characters of the name accepted
from the user. for example :
Enter Your Name : Suman
Series is :
0 S
1 u
2 m
3 a
4 n
Ans.
import pandas as pd
nm = input("Enter your name : ")
S1 = pd.Series(list(nm))
print(S1)
OUTPUT :
Enter your name : amit
0 a
1 m
2 i
3 t
dtype: object
Q63. Write a program to arrange the given Pandas Series in increasing order.
0 150
1 252
2 85
3 420
4 111
5 275
Ans.
import pandas as pd
S1 = pd.Series([150,252,85,420,111,235])
print("Original Series is : ")
print(S1)
print("New Series is : ")
print(S1.sort_values())
OUTPUT :
Original Series is :
0 150
1 252
2 85
3 420
4 111
5 235
dtype: int64
New Series is :
2 85
4 111
0 150
5 235
1 252
3 420
dtype: int64
Q64. Write a program to add a new value (Accept that value from the user) in the given
Pandas Series.
0 150
1 252
2 85
3 420
4 111
5 275
Ans.
import pandas as pd
S1 = pd.Series([150, 252, 85, 420, 111, 235])
v = int(input("Enter any value : "))
print("Original Series is : ")
print(S1)
S1[6]=v
print("New Series is : ")
print(S1)
OUTPUT :
New Series is :
0 150
1 252
2 85
3 420
4 111
5 235
6 677
dtype: int64
Q65. Write a program to display the sum and average of all the values in the given Pandas
Series.
0 15
1 2
2 8
3 4
4 1
5 5
6 14
Ans.
import pandas as pd
S1 = pd.Series([15, 2, 8, 4, 1, 5, 14])
print("Sum is : ", sum(S1.values))
print("Average is : ", sum(S1.values)/S1.count( ))
OUTPUT :
Sum is : 49
Average is : 7.0
Q66. Write a program to find the minimum, maximum value of given Pandas Series.
0 15
1 2
2 8
3 4
4 1
5 5
6 14
Ans.
import pandas as pd
S1 = pd.Series([15,2,8,4,1,5,14])
print("Maximum value is : ",max(S1.values))
print("Minimum value is : ",min(S1.values))
OUTPUT :
Maximum value is : 15
Minimum value is : 1
Q67. Write a program to display multiple of 5 from the given Pandas Series.
0 15
1 2
2 8
3 4
4 1
5 25
6 30
Ans.
import pandas as pd
S1 = pd.Series([15, 2, 8, 4, 1, 25, 30])
print(S1[S1.values%5==0])
OUTPUT :
0 15
5 25
6 30
dtype: int64
Q68. Write a program in python to display all values of given pandas series with first
character in upper case. for example
Original Series is :
0 ravi
1 ram
2 sonu
3 david
Expected Output :
0 Ravi
1 Ram
2 Sonu
3 David
dtype: object
Ans.
import pandas as pd
S1 = pd.Series(["ravi", "ram", "sonu", "david"])
L = S1.values
L1=[ ]
for i in L:
L1.append(i[0].upper() + i[1:])
S2 = pd.Series(L1)
print(S2)
Q69. Write a program to display all odd numbers and its sum from the given Pandas
Series.
0 15
1 2
2 8
3 4
4 1
5 25
6 30
Ans.
import pandas as pd
S1 = pd.Series([15, 2, 8, 4, 1, 25, 30])
print(S1[S1%2!=0])
S2=S1[S1%2!=0]
print("Sum of all odd numbers is : ", sum(S2.values))
OUTPUT :
0 15
4 1
5 25
dtype: int64
Ans.
0 1
1 2
2 25
3 35
4 45
5 55
6 65
7 75
8 85
dtype: int64
Ans.
25 35
35 45
45 55
55 65
65 75
75 85
85 95
dtype: int64
Q72. Write the output of the following :
import pandas as pd
S1 = pd.Series(range(1, 20, 3))
print(S1)
Ans.
0 1
1 4
2 7
3 10
4 13
5 16
6 19
dtype: int64