Skip to content
Question 35 of 38

Q.Write a Python program to create a series object, country using a list that stores the capital of each country. Note: Assume four countries to be used as index of the series object are India, UK, Denmark, and Thailand having their capitals as New Delhi, London, Copenhagen, and Bangkok respectively.

Uttarakhand UbseCBSE Class XII Board 2023Subjective· 2mImportance★★★★★
92% · 35/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 →

This question asks you to create a Pandas Series object in Python where the index labels are country names and the values are their respective capitals, using a list of capitals and a separate list of index labels.

The core idea here is that a Pandas Series is like a labelled column of data — think of it as a hybrid between a list and a dictionary. You can create one from a plain Python list, but then you need to tell Pandas what labels (the index) to attach to each value. In this problem, the values are the capitals, and the labels are the country names.

Let’s set the scene. You have four countries: India, UK, Denmark, and Thailand. Their capitals are New Delhi, London, Copenhagen, and Bangkok respectively. The question says to use a list that stores the capital of each country. That means your primary data — the values of the Series — will come from a list like ["New Delhi", "London", "Copenhagen", "Bangkok"]. But a list alone has no labels; it only has positional indices (0, 1, 2, 3). To turn this into a meaningful Series where you can look up a capital by country name, you need to supply the index separately.

In Pandas, when you create a Series from a list, you can pass an index parameter. That index parameter should be another list of the same length, containing the labels you want. Here, that index list would be ["India", "UK", "Denmark", "Thailand"].

Note

The order matters. Pandas pairs the first element of the data list with the first element of the index list, the second with the second, and so on. So if you accidentally swap the order of countries in the index, the capitals will be mismatched.

Now, the actual Python code is straightforward. You first need to import the Pandas library — conventionally done as import pandas as pd. Then you create the two lists: one for capitals, one for countries. Finally, you call pd.Series() with the data list as the first argument and the index list as the index argument.

Here is the complete program:

import pandas as pd

# List of capitals (the values)
capitals = ["New Delhi", "London", "Copenhagen", "Bangkok"]

# List of country names (the index labels)
countries = ["India", "UK", "Denmark", "Thailand"]

# Create the Series object
country_series = pd.Series(capitals, index=countries)

# Display the Series
print(country_series)

When you run this, the output will look like:

India         New Delhi
UK              London
Denmark      Copenhagen
Thailand       Bangkok
dtype: object
``` …

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.