Chapter 2
Data Handling using Pandas – I
Exercise
1. What is a Series and how is it different from a 1-D array, a list and a dictionary?
Answer:
2. What is a DataFrame and how is it different from a 2-D array?
Answer:
3. How are DataFrames related to Series?
Answer:
4. What do you understand by the size of
(i) a Series,
(ii) a DataFrame?
Answer:
5. Create the following Series and do the specified operations:
a) EngAlph, having 26 elements with the alphabets as values and default index values.
Answer:
b) Vowels, having 5 elements with index labels ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’ and all the five values set to zero. Check if it is an empty series.
Answer:
c) Friends, from a dictionary having roll numbers of five of your friends as data and their first name as keys.
Answer:
d) MTseries, an empty Series. Check if it is an empty series.
Answer:
e) MonthDays, from a numpy array having the number of days in the 12 months of a year. The labels should be the month numbers from 1 to 12. 6. Using the Series created in Question
Answer:
5, write commands for the following:
a) Set all the values of Vowels to 10 and display the Series.
Answer:
b) Divide all values of Vowels by 2 and display the Series.
Answer:
c) Create another series Vowels1 having 5 elements with index labels ‘a’, ‘e’, ‘i’, ‘o’ and ‘u’ having values [2,5,6,3,8] respectively.
Answer:
d) Add Vowels and Vowels1 and assign the result to Vowels3.
Answer:
e) Subtract, Multiply and Divide Vowels by Vowels1.
Answer:
f) Alter the labels of Vowels1 to [‘A’, ‘E’, ‘I’, ‘O’, ‘U’]. 7. Using the Series created in Question
Answer:
5, write commands for the following:
a) Find the dimensions, size and values of the Series EngAlph, Vowels, Friends, MTseries, MonthDays.
Answer:
b) Rename the Series MTseries as SeriesEmpty.
Answer:
c) Name the index of the Series MonthDays as monthno and that of Series Friends as Fname.
Answer:
d) Display the 3rd and 2nd value of the Series Friends, in that order.
Answer:
e) Display the alphabets ‘e’ to ‘p’ from the Series EngAlph.
Answer:
f) Display the first 10 values in the Series EngAlph.
Answer:
g) Display the last 10 values in the Series EngAlph.
Answer:
h) Display the MTseries.
Answer:
8. Using the Series created in Question
Answer:
5, write commands for the following:
a) Display the names of the months 3 through 7 from the Series MonthDays.
Answer:
b) Display the Series MonthDays in reverse order.
Answer:
9. Create the following DataFrame Sales containing year wise sales figures for five sales persons in INR. Use the years as column labels, and sales person
names as row labels.
10. Use the DataFrame created in Question 9 above to do the following:
a) Display the row labels of Sales.
b) Display the column labels of Sales.
c) Display the data types of each column of Sales.
d) Display the dimensions, shape, size and values of Sales.
e) Display the last two rows of Sales.
f) Display the first two columns of Sales.
g) Create a dictionary using the following data. Use this dictionary to create a DataFrame Sales2.D
h) Check if Sales2 is empty or it contains data.
Answer:
11. Use the DataFrame created in Question 9 above to do the following:
a) Append the DataFrame Sales2 to the DataFrame Sales.
Answer:
b) Change the DataFrame Sales such that it becomes its transpose.
Answer:
c) Display the sales made by all sales persons in the year 2017.
Answer:
d) Display the sales made by Madhu and Ankit in the year 2017 and 2018.
Answer:
e) Display the sales made by Shruti 2016.
Answer:
f) Add data to Sales for salesman Sumeet where the sales made are [196.2, 37800, 52000, 78438, 38852] in the years [2014, 2015, 2016, 2017, 2018] respectively.
Answer:
g) Delete the data for the year 2014 from the DataFrame Sales.
Answer:
h) Delete the data for sales man Kinshuk from the DataFrame Sales.
Answer:
i) Change the name of the salesperson Ankit to Vivaan and Madhu to Shailesh.
Answer:
j) Update the sale made by Shailesh in 2018 to 100000.
Answer:
k) Write the values of DataFrame Sales to a comma separated file SalesFigures.csv on the disk. Do not write the row labels and column labels.
Answer:
l) Read the data in the file SalesFigures.csv into a DataFrame SalesRetrieved and Display it. Now update the row labels and column labels of SalesRetrieved to be the same as that of Sales.
Answer: