Skip to content
Question 28 of 38

Q.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

Yanam BieapCBSE Class XII Board 2024MCQ· 1mImportance★★★★★
74% · 28/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 →

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.

  1. Importing Pandas:

    import pandas as pd
    

    This line imports the Pandas library and assigns it the conventional alias pd. This allows us to use Pandas functions and classes by prefixing them with pd..

  2. Creating a Dictionary:

    dd={'One':1,'Two':2,'Three':3,'Seven':7}
    

    Here, a standard Python dictionary named dd is 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'.

  3. Creating a Pandas Series from the Dictionary:

    rr=pd.Series(dd)
    

    This line converts the dictionary dd into a Pandas Series named rr. The keys of dd ('One', 'Two', 'Three', 'Seven') become the index of rr, and their corresponding values (1, 2, 3, 7) become the data. Since dictionaries preserve insertion order, the Series rr will initially look like this:

    One      1
    Two      2
    Three    3
    Seven    7
    dtype: int64
    
    Important

    Python 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.

  4. Adding a New Element to the Series:

    rr['Four']=4
    ``` …
    

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.