0% found this document useful (0 votes)
18 views

series

The document contains a series of true/false statements and multiple-choice questions related to the Pandas library in Python. It covers topics such as the use of the head() function, Series attributes, and methods for data manipulation. Additionally, it includes programming exercises and code snippets for creating and manipulating Pandas Series and DataFrames.

Uploaded by

28kpclasses
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

series

The document contains a series of true/false statements and multiple-choice questions related to the Pandas library in Python. It covers topics such as the use of the head() function, Series attributes, and methods for data manipulation. Additionally, it includes programming exercises and code snippets for creating and manipulating Pandas Series and DataFrames.

Uploaded by

28kpclasses
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

(True/False)

1. State whether the following statement is True or False:

In Pandas, the head() function is used to display the last few rows of a

Series.

Ans : False

2. The only alias name that can be used with the Pandas library is pd.

Ans False

3. Slicing can be used to extract a specific portion from a Pandas Series.

Ans : True

Multiple Choice Questions

Q.1 By default the Series indices start with:

(a) 1 (b) 0 (c) ‘A’ (d) ‘a’

Ans : b

Q.2 _________ is a series attribute used to check whether a series contains any value
or not:

A) hasnans B) blank C) empty D) null

Ans : a)

Q.3 Which of the following statement will import pandas library ?

a) Import pandas as pd
b) import Pandas as py
c) import pandas as pd
d) import panda as pd
ans: c)

Q.4 To get the number of elements in a Series object, ............... attribute may be
used.

a) Index b) size c) itemsize d) ndim


Answer b)size
Q. 5 To get the size of the datatype of the items in Series object, you can
display ............... attribute.

a) Index b) size c) itemsize d) ndim


Answer : c) itemsize

Q. 6 To display third element of a Series object S, you will write ............... .

a) S[:3] b) S[2] c) S[3] d) S[:2]

Answer S[2]

Q. 7 Which is a label based data selecting method to select a specific row(s) or


column(s)

which we want to select?

(A).iloc() (B) .loc() (C) .head() (D).tail()

Ans : .loc()

Q.8 In Python Pandas, while performing mathematical operations on series, index


matching is

implemented and all missing values are filled in with _____by default.

(A)Null (B) Blank (C) NaN (D) Zero

Ans : NaN

Q.9 Write a Pandas program to compare the elements of the two Pandas Series.

Sample Series: [2, 4, 6, 8, 10], [1, 3, 5, 7, 10]

Q.10 . Write a Pandas program to convert a dictionary to a Pandas series and do the
following:

(i) Retrieve the third element and print it

(ii) Retrieve and print the first three elements

(iii) Retrieve and print the last two elements

(iv) Retrieve and print alternate elements, starting from index ‘b’.

Q11. What is not true about Series in pandas.


a. With labelled indexes the value at the end index label is excluded in the

output.

b. A Series is a one-dimensional array containing a sequence of values.

c. Each value has a data label associated with it also called its index.

d. Positional index takes an integer value that corresponds to its position

in the series starting from 0.

Pandas Series Programs

Q.1. Write python code to create following series which stores employee names as

index and their respective salary as data.

Meet 50000

Ram 65000

Smita 68500

Rohan 46000

Riddhi 56000

Ans:

import pandas as pd

name=[‘ Meet’,’ Ram’,’ Smita’,’ Rohan‘,’ Riddhi’]

s=pd.Series([50000,65000,68500,46000,56000],index=name)

OR

import pandas as pd

d1={‘acc’:98,’bst’:95,’eco’:88}

d2={‘acc’:94,’bst’:87,’eco’:79}

d3={‘acc’:89,’bst’:76,’eco’:81}

df=pd.DataFrame([d1,d2,d3],index=[‘Meet’,’Ram’,’Smita’])

print(df)

Q2. Observe the following code and predict the output.


import pandas as pd

l1=[10,20,30,40,50]

l2=[89,56,23,45,78]

s1=pd.Series(l1,index=[‘a’,’b’,’c’,’d’,’e’])

s2=pd.Series(l2,index=[‘a’,’b’,’e’,’g’,’k’])

print(s1+s2)

print(s2*2)

print(s1[s1>25])

Q.3 . Create a series with 5 elements, then find and print the square of each even
element

and cube of each odd element.

Ans:

Q.4 Write a program to create a series object books using a list that stores the
number of

quantity of books in library of your school.

Note: Assume four books’ names as index are [“Computer World”, “Fun with

Mathematics”, “Artificial Intelligence”, “universe of Physics” having quantities as 30,


25,

20, 2 respectively and also import pandas library properly.

Ans : import pandas as pd

lib=['Computer World', 'Fun with Mathematics', 'Artificial

Intelligence', 'Universe of Physics']

data=[30, 25, 20, 2]


books=pd.Series(data, index=lib)

print(books)

1⁄2 mark for any correct statement.

Q. Write a program to create a series object F1 using a dictionary that stores

the number of furniture in each lab of your school.

Note: Assume four furniture names are Table, Sofa, Chair and stool having

40, 2, 45, 26 items respectively and pandas library has been imported as

pd.

import pandas as pd

F={‘Table’ :40, ‘Sofa’ :2, ‘Chair’ :45, ‘Stool’ :26}

F1=pd.Series(St)

Carefully observe the following code:

import pandas as pd

data = {'col1' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),

'col2' : pd.Series([0, 78, -25, 13], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(data)

print(df)

Answer the following based on above code:

a) Print no. of rows and columns

b) print alternate rows and all the columns in reverse order

Q. Consider the following code. Write appropriate words to complete

Line1: import pandas as pd

Line2: import___________ # Library name

Line3: A=np.______(2,11,2) # function name to get numpy array


Line 4: S=pd.Series(______, Index=[________]) # Data name and indexes

Line 5: Print(S)

Ans:

Line1: import pandas as pd

Line2: import_numpy as np # Library name

Line3: A=np.array([2,11,2]) # function name to get numpy array

Line 4: S=pd.Series(_A_____, index=[_0,1,2]) # Data name and indexes

Line 5: print(S)

The Python code written below has syntactical errors. Rewrite the correct code and

underline the correction(s) made.

Find the output of the following Python code :

import pandas as pd

vaccine_qty=pd.Series([10,16,1],index=["Typhoid","Tetanus","Hepatitis"])
cost=pd.Series([200,500,800],index=["Typhoid","Tetanus","Flu"])

print(vaccine_qty + cost)

Ans: Flu NaN

Hepatitis NaN

Tetanus 516.0

Typhoid 210.0

dtype: float64

Q. Write commands to print following details of a Series object seal :

(a) if the series is empty

(b) indexes of the series

(c) The data type of underlying data

(d) if the series stores any NaN values

Ans : (a) seal.empty

(b) seal.index

(c) seal.dtype

(d) seal.hasnans

Q.

a. What will be the output of the following code:

import pandas as pd

A=pd.Series(data=[10,13,22,18])

print(A>15)

ans:

Output will be :

0 False
1 False

2 True

3 True

Q. Complete the given Python code to get the required output as: Rajasthan

import _________ as pd

di = {'Corbett': 'Uttarakhand', 'Sariska': 'Rajasthan', 'Kanha': 'Madhya Pradesh’,

'Gir':'Gujarat'}

NP = ___________. Series( _____ )

print(NP[ ___________ ])

ans:

import pandas as pd

di = {'Corbett': 'Uttarakhand', 'Sariska':'Rajasthan', 'Kanha': 'Madhya


Pradesh','Gir':'Gujarat'}

NP = pd.Series( di)

print(NP[ 'Sariska'])

Q. What will be the output of the following:

import pandas as pd

x= [20, 40,90, 110]

y=pd.Series([20, 40,90, 110])

print (x*2)

print(y*2)

Output of First print()

[20, 40, 90, 110, 20, 40, 90, 110]

Output of Second print()

0 40

1 80

2 180
3 220

dtype: int64

b. Create a Series monthdays in which name of the months are index and
corresponding number of days are data.

import pandas as pd

monthnames=['JAN','FEB','MAR','APRIL','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']

days=[31,28,31,30,31,30,31,31,30,31,30,31]

monthdays = pd.Series(days, index=monthnames)

print(monthdays)

Find the error and also explain the reason of that error in following code fragment :

S2 = pd.Series([101, 102, 102, 104])

print(S2.index)

S2.index = [0, 1, 2, 3, 4, 5]

S2[5] = 220

print(S2)

ans: Length of series is not equal to length of index. There is no row of index 5

Q. Consider the following series object S_amt

Table 350

Chair 200

Sofa 800

Stool 150

Write code for the following:

I.To display the name of the furniture having rent > 250

II.To name the series object as Furniture


Ans : I. print(S_amt[S_amt>250])

II. S_amt.name= ‘Furniture’

Q.

Ans: import pandas as pd

data = {

'India': 'Rohit Sharma',

'South Africa': 'Aiden Markram',

'England': 'Jos Buttler',

'Afghanistan': 'Rashid Khan'

series = pd.Series(data)

print(series)

Q.

Ans : import pandas as pd

data = {
'Uttar Pradesh': Kanpur,

'Maharashtra': 'Mumbai',

'Bihar': 'Patna',

'West Bengal': 'Kolkata',

'Tamil Nadu': 'Chennai'

series = pd.Series(data)

print(series)

Assertion (A): A Series stores data row-wise.

Reason (R): A Series is a one-dimensional labelled data structure.

Ans : (d) A is False but R is True

You might also like