Q.(a) Write a Python program to create and display a Pandas Series as shown below : July 31 Aug 31 Sept 30 Oct 31 The index labels are the names of the months – July, Aug, Sept, Oct. The corresponding scalar values are the number of days in these respective months – 31, 31, 30, 31.
🔒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 →Part (a)Concept understanding — Pandas Series Creation
Imagine you have a messy desk with loose notes, receipts, and to-do lists scattered everywhere. Now imagine you take a single strip of paper, write one item on each line in a fixed order, and label the strip "Monday's Expenses." That strip is a Pandas Series — a single column of data with a label for each row.
A Pandas Series is the simplest building block in the Pandas library for Python. It is a one-dimensional array that can hold any type of data — numbers, text, dates, or even mixed types — and it comes with an index, which is like a row number or a custom label for each entry. Think of it as a smart list that remembers the name of each item.
A Series is not a table or a spreadsheet. It is just one column of data, with an index on the left and values on the right. If you have ever used a dictionary in Python, a Series is similar — keys on the left, values on the right — but with extra powers like sorting, filtering, and mathematical operations.
Why does this matter for a commerce or humanities student?
In your work, you will often deal with lists of things: monthly sales figures, student names in a class, dates of transactions, or categories of expenses. A Series lets you store that list in a structured way so you can quickly find, sort, or analyse it. For example, if you have a list of daily footfall in a store, a Series lets you label each day (Monday, Tuesday, …) and then instantly ask: "Which day had the highest footfall?" or "What was the average footfall on weekends?"
How is a Series created?
You can create a Series from several everyday sources:
- From a Python list: You give Pandas a simple list of values, and it automatically assigns numeric indices (0, 1, 2, …) just like the rows in a notebook.
- From a Python dictionary: The dictionary keys become the index labels, and the values become the data. This is very natural — you already think in key-value pairs (e.g., "January": 45000, "February": 52000).
- From a single value: You can create a Series where every entry is the same value, repeated a certain number of times — useful for setting a default or a constant.
- From a NumPy array (if you have done some data science): but for a first meeting, the list and dictionary methods are the most intuitive.
The index is the backbone of a Series. Without an index, you just have a plain list. With an index, you can label your data meaningfully — dates, names, categories — and perform operations that respect those labels. For instance, adding two Series with the same index will align them by label, not by position.
A concrete mental picture …
Part (b)Concept understanding — DataFrame Creation
DataFrame Creation: A First Look
Think of a spreadsheet you might use to track your monthly expenses. You have rows — one for each day or each purchase — and columns with labels like "Date", "Item", "Category", "Amount", and "Payment Method". That grid of labelled rows and columns is exactly what a DataFrame is in computing. It is a two-dimensional, table-like structure for storing and working with data.
The word "DataFrame" comes from two ideas: data (the information itself) and frame (the structure that holds it together). You can imagine it as a digital filing cabinet where every drawer (column) has a name, every folder (row) has a number, and every cell contains a single piece of information — a date, a word, a number, or even a missing entry.
The Core Idea
A DataFrame is not just a random collection of numbers and text. It is an organised table where:
- Each column represents a specific attribute or variable — for example, "Product Name", "Quantity Sold", "Price per Unit".
- Each row represents a single observation or record — one sale, one customer, one transaction.
- Every column has a label (its name), and every row has an index (its position or label).
This organisation is what makes a DataFrame powerful. You can ask questions like "Show me all rows where the category is 'Groceries'" or "What is the average amount spent in March?" — and the structure gives you a clear, fast way to get answers.
How a DataFrame Is Created
Creating a DataFrame means taking raw data and arranging it into this labelled table. The data can come from many places:
- A list of dictionaries, where each dictionary is one row and the keys become column names.
- A dictionary of lists, where each key is a column name and the list holds the values for that column.
- A CSV file (a comma-separated values file) that you read from your computer.
- Data entered manually, row by row.
The key point is that creation always involves two decisions: what the column names will be, and what data goes under each column. The row index is usually assigned automatically (0, 1, 2, …) unless you specify your own labels.
Why It Matters
For a commerce or humanities student, the DataFrame is the single most useful tool for working with real-world data. Think of a small business tracking its inventory: the owner needs to see product names, stock levels, reorder dates, and supplier details all in one place. A DataFrame lets them do that, and then filter, sort, or summarise the information without rewriting everything by hand.
In academic work — analysing survey responses, comparing economic indicators across years, or studying historical records — the DataFrame turns messy, scattered data into a clean, queryable table. You can add new columns (like "Profit" calculated from "Revenue" minus "Cost"), remove rows that are incomplete, or merge two tables on a common column (like matching customer IDs from a sales table with names from a customer table).
A DataFrame is not the same as a simple list or a plain table in a word processor. It is a structured object that understands its own columns, rows, and data types. This understanding is what allows you to perform operations — like finding the total sales for a region — with a single command, rather than manually scanning hundreds of rows. …
Part (a)
Create a Series with the month names as the index and the day counts as the values.
import pandas as pd
months = ['July', 'Aug', 'Sept', 'Oct']
days = [31, 31, 30, 31]
s = pd.Series(days, index=months)
print(s)
Expected output:
July 31
Aug 31
Sept 30
Oct 31
dtype: int64 …
Part (a): build a Series with pd.Series(days, index=months) mapping July/Aug/Sept/Oct to 31/31/30/31.
Part (b): pd.read_csv('customer.csv') loads the file into DF; DF.tail(2) shows the last two rows.
Part (a)
A Series is a one-dimensional labelled array. Pass the values as the data and the month names via the index parameter.
import pandas as pd
months = ['July', 'Aug', 'Sept', 'Oct']
days = [31, 31, 30, 31]
s = pd.Series(days, index=months)
print(s)
Expected output:
July 31
Aug 31
Sept 30
Oct 31
dtype: int64
``` …
- CBSE 2025Set 90/1/11 markQ.State whether the following statement is True or False: In Python, we cannot create an empty DataFrame.
›Reveal solutionSolution
Python's pandas library allows you to create an empty DataFrame with no rows or columns using
pd.DataFrame(). The statement is False.Why DataFrames can be empty
A DataFrame in pandas is a two-dimensional labeled data structure, essentially a table with rows and columns. Like any container in programming, it doesn't need to start with content. You can initialize an empty DataFrame and populate it later as data becomes available—this is common when building up results iteratively or when you need a structure ready before data arrives.
The flexibility to create empty DataFrames is actually quite useful in practice. You might start with an empty frame and add columns one by one, or append rows as you process data in a loop.
Creating an empty DataFrame
Here are the standard ways to create an empty DataFrame in pandas:
-
Completely empty (no rows, no columns):
import pandas as pd df = pd.DataFrame()This gives you a DataFrame with shape
(0, 0). -
Empty with predefined columns:
df = pd.DataFrame(columns=['Name', 'Age', 'City'])This creates a DataFrame with three columns but zero rows—shape
(0, 3). The structure is ready; you just need to add data. -
Empty with a specific index:
df = pd.DataFrame(index=range(5)) ``` …
-
- CBSE 2025Set 90/1/11 markMCQQ.What is the default index type for a Pandas Series if not explicitly specified? (A) String (B) List (C) Numeric (D) Boolean
›Reveal solutionSolution
If you don't specify an index when creating a Pandas Series, Pandas automatically assigns a numeric index starting from 0. The correct option is (C).
The key idea here is that Pandas is designed to work like a labelled array. When you don't provide labels, it falls back to the simplest, most universal labelling system: integers. This is exactly what NumPy arrays do by default — Pandas just carries that convention forward.
Let's see why this happens and why the other options don't fit.
-
What happens when you create a Series without an index?
When you write something like
pd.Series([10, 20, 30]), you haven't passed anindexparameter. Pandas sees this and says: "No labels given? I'll make my own." It generates aRangeIndexfrom 0 ton-1, wherenis the number of elements. So for three elements, the index becomes[0, 1, 2]. -
Why numeric and not string, list, or boolean?
- String (A): Strings are possible if you explicitly pass
index=['a','b','c'], but they are not the default. Pandas doesn't guess string labels — that would be arbitrary and unpredictable. - List (B): A list is a Python data structure, not a data type for an index. The index itself can be a list object, but the type of the index entries is what matters. The default entries are integers, not lists.
- Boolean (D): Booleans (
True,False) could technically be used as index labels, but they are not the default. If Pandas used booleans, you'd only ever have two possible labels — that would be useless for more than two elements.
- String (A): Strings are possible if you explicitly pass
-
What is the actual default type?
The default index is a
RangeIndex, which is a special kind of integer index. It behaves like a sequence of integers starting at 0, stepping by 1. So the type of each label isint(numeric). This is why option (C) is correct. …
-
- CBSE 2024Set 90/1/11 markMCQQ.What will be the output of the following Python code ? import pandas as pd dd={'One':1,'Two':2,'Three':3,'Seven':7} rr=pd.Series(dd) rr['Four']=4 print(rr) (A) One 1 Two 2 Three 3 Seven 7 dtype: int64 (B) One 1 Two 2 Three 3 Four 4 Seven 7 dtype: int64 (C) Four 4 One 1 Two 2 Three 3 Seven 7 dtype: int64 (D) One 1 Two 2 Three 3 Seven 7 Four 4 dtype: int64
›Reveal solutionSolution
A Pandas Series created from a dictionary preserves the dictionary's insertion order, and new elements added to the Series are appended to its end. The output will be (D).
A Pandas Series is a fundamental data structure in the Pandas library, representing a one-dimensional labeled array capable of holding any data type. Think of it like a column in a spreadsheet or a single column of data in a database table. Each item in a Series has a value and an associated label, called its index.
When you create a Pandas Series from a Python dictionary, a direct mapping occurs: the dictionary's keys become the Series' index labels, and the dictionary's values become the Series' data. A crucial point here, especially in modern Python (version 3.7 and later, which is standard for competitive programming and most environments), is that dictionaries maintain insertion order. This means the order in which you define key-value pairs in a dictionary is the order they will be stored and retrieved.
When you add a new element to an existing Series using an assignment like
series_name[new_index] = new_value, this new key-value pair is always appended to the end of the Series. It does not attempt to insert itself alphabetically or based on any other sorting logic unless you explicitly sort the Series afterwards.Let's trace the code step by step.
-
Importing Pandas:
import pandas as pdThis line imports the Pandas library and assigns it the conventional alias
pd. This allows us to use Pandas functions and classes by prefixing them withpd.. -
Creating a Dictionary:
dd={'One':1,'Two':2,'Three':3,'Seven':7}Here, a standard Python dictionary named
ddis created. It contains four key-value pairs. Due to Python's dictionary behavior (since version 3.7), the order of these elements is preserved as: 'One', 'Two', 'Three', 'Seven'. -
Creating a Pandas Series from the Dictionary:
rr=pd.Series(dd)This line converts the dictionary
ddinto a Pandas Series namedrr. The keys ofdd('One', 'Two', 'Three', 'Seven') become the index ofrr, and their corresponding values (1, 2, 3, 7) become the data. Since dictionaries preserve insertion order, the Seriesrrwill initially look like this:One 1 Two 2 Three 3 Seven 7 dtype: int64ImportantPython dictionaries (from version 3.7 onwards) preserve the order of insertion. When a Pandas Series is created from such a dictionary, it respects this order.
-
Adding a New Element to the Series:
rr['Four']=4 ``` …
-
- CBSE 2023Set 90/1/11 markMCQQ.What will be the output of the following code? import pandas as pd myser = pd.Series([0, 0, 0]) print(myser) (A) 0 0 0 0 0 0 (B) 0 1 0 1 0 2 (C) 0 0 1 0 2 0 (D) 0 0 1 1 2 2
›Reveal solutionSolution
pd.Series([0, 0, 0])prints each element asindex valueon its own line, giving0 0,1 0,2 0— reading straight across, that's0 0 1 0 2 0, option (C).Trace
import pandas as pd myser = pd.Series([0, 0, 0]) print(myser)pd.Series([0, 0, 0])builds a 1-D labelled array from the list[0, 0, 0].- Since no index is supplied, pandas assigns the default RangeIndex:
0, 1, 2. - Each element keeps the value
0from the input list.
So the Series looks like:
index value 0 0 1 0 2 0 Actual output
0 0 1 0 2 0 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.