Informatics Practices
Consider two objects x and y. x is a list whereas y is a Series. Both have values 20, 40, 90, 110.
What will be the output of the following two statements considering that the above objects have been created already ?
(a) print (x*2)
(b) print (y*2)
Justify your answer.
Python Pandas
5 Likes
Answer
(a)
Output
[20, 40, 90, 110, 20, 40, 90, 110]
In the first statement, x represents a list. When a list is multiplied by 2 (x*2), it replicates each element of the list twice.
(b)
Output
0 40
1 80
2 180
3 220
dtype: int64
In the second statement, y represents a Series. When a Series is multiplied by a value, each element of the Series is multiplied by 2, as Series supports vectorized operations.
Answered By
2 Likes
Related Questions
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
Given the following Series S1 and S2 :
S1 A 10 B 40 C 34 D 60 S2 A 80 B 20 C 74 D 90 Write the command to find the sum of series S1 and S2.
Given a dataframe df as shown below :
A B D 0 15 17 19 1 16 18 20 2 20 21 22 What will be the result of following code statements ?
(a) df['C'] = np.NaN
(b) df['C'] = [2, 5]
(c) df['C'] = [12, 15, 27]
Write code statements to list the following, from a dataframe namely sales:
(a) List only columns 'Item' and 'Revenue'.
(b) List rows from 3 to 7.
(c) List the value of cell in 5th row, 'Item' column.