Skip to content
Exercises · Q9
Q.

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.

2014201520162017
Madhu100.5120002000050000
Kusum150.8180005000060000
Kinshuk200.9220007000070000
Ankit300003000010000080000
Shruti400004500012500090000
CBSENCERTSubjective· 3mImportance★★★★★
45% · 17/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 →

Create a pandas DataFrame with sales person names as row labels (index) and years as column labels, populated with the given sales figures.

Why a DataFrame?

A DataFrame is the right structure here because we have tabular data with meaningful labels on both axes. The years (2014–2017) naturally form column headers, and the sales person names serve as row identifiers. This two-dimensional labeled structure makes it trivial to query sales by person, by year, or both — far more intuitive than nested lists or a plain NumPy array.

The Construction Strategy

Pandas offers several ways to build a DataFrame. For data already organized in rows (one row per sales person), the dictionary-of-lists approach is cleanest: each key is a column name (year), and its value is a list of sales figures in the order of the sales persons. We then pass the index parameter to label the rows.

import pandas as pd

# Define the sales data as a dictionary: keys are years (columns), values are lists of sales figures
data = {
    2014: [100.5, 150.8, 200.9, 30000, 40000],
    2015: [12000, 18000, 22000, 30000, 45000],
    2016: [20000, 50000, 70000, 100000, 125000],
    2017: [50000, 60000, 70000, 80000, 90000]
}

# Row labels (sales person names) in the same order as the data lists
index = ['Madhu', 'Kusum', 'Kinshuk', 'Ankit', 'Shruti']

# Create the DataFrame
Sales = pd.DataFrame(data, index=index)

print(Sales)

Expected Output:

          2014   2015    2016   2017
Madhu     100.5  12000   20000  50000
Kusum     150.8  18000   50000  60000
Kinshuk   200.9  22000   70000  70000
Ankit   30000.0  30000  100000  80000
Shruti  40000.0  45000  125000  90000

Key Points in the Code

data dictionary: Each year is a key, and the corresponding list contains sales figures for all five people in the same order. The first element in every list is Madhu's sales for that year, the second is Kusum's, and so on.

index parameter: This assigns the sales person names as row labels. Without it, pandas would default to integer indices (0, 1, 2, 3, 4), losing the semantic meaning. …

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.