Skip to content
Exercises · Q10
Q.

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.

2018
Madhu160000
Kusum110000
Kinshuk500000
Ankit340000
Shruti900000

h) Check if Sales2 is empty or it contains data.

CBSENCERTSubjective· 5mImportance★★★★★
47% · 18/38 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Inspect the Sales DataFrame (from Question 9) using its built-in attributes and slicing methods, then build a second DataFrame Sales2 from a one-column dictionary.

Sales has row labels Madhu/Kusum/Kinshuk/Ankit/Shruti and column labels 2014–2017 (values 100.5, 12000, 20000, 50000 for Madhu, and so on, exactly as created in Question 9).

a) Row labels — Sales.index returns an Index object listing the five sales-person names.

b) Column labels — Sales.columns returns an Index of the four years.

c) Data types — Sales.dtypes shows one dtype per column. The 2014 column is float64 because it contains the decimal values 100.5/150.8/200.9 for the first three rows (Pandas upgrades the whole column to float once any value in it is a float); the other three columns are int64.

d) Dimensions, shape, size, values — Sales.ndim is 2 (a DataFrame is always 2-D); Sales.shape is (5, 4) — 5 rows, 4 columns; Sales.size is 20 (5 × 4); Sales.values returns the same 20 numbers as a plain NumPy 2-D array, without the row/column labels.

e) Last two rows — Sales.tail(2) returns the Ankit and Shruti rows.

f) First two columns — Sales.iloc[:, :2] (position-based column slicing) returns the 2014 and 2015 columns for every row.

g) Sales2 — built from a dictionary with one key (2018) whose value is the list of five 2018 sales figures, using the same row index as Sales.

h) Empty check — Sales2.empty is False because the DataFrame actually holds 5 rows of data.

# Sales is the DataFrame created in Question 9
print(Sales.index)        # a) row labels
print(Sales.columns)      # b) column labels
print(Sales.dtypes)       # c) data type of each column
print(Sales.ndim)         # d) dimensions
print(Sales.shape)        #    shape
print(Sales.size)         #    size
print(Sales.values)       #    values
print(Sales.tail(2))      # e) last two rows
print(Sales.iloc[:, :2])  # f) first two columns

# g) Sales2, from the given dictionary
data2 = {2018: [160000, 110000, 500000, 340000, 900000]}
Sales2 = pd.DataFrame(data2, index=['Madhu', 'Kusum', 'Kinshuk', 'Ankit', 'Shruti'])
print(Sales2)
print(Sales2.empty)       # h)

Output:

a) Index(['Madhu', 'Kusum', 'Kinshuk', 'Ankit', 'Shruti'], dtype='object')

b) Index([2014, 2015, 2016, 2017], dtype='int64') …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.