Question:

Write a Python program to create the following DataFrame using a Dictionary of Series:
Expected DataFrame:
   City         State
0  Mumbai       Maharashtra
1  Dehradun     Uttarakhand
2  Bengaluru    Karnataka
3  Hyderabad    Telangana

Show Hint

When you have columns as separate Series, combining them in a dictionary is the easiest way to build a DataFrame.
Updated On: Jul 14, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

To create this DataFrame using a Dictionary of Series, we can define each column as a Series first and then combine them into a DataFrame.
Here is the correct Python code:
import pandas as pd

city_series = pd.Series(['Mumbai', 'Dehradun', 'Bengaluru', 
                'Hyderabad'])
state_series = pd.Series(['Maharashtra', 'Uttarakhand', 'Karnataka',
                 'Telangana'])

data = {'City': city_series, 'State': state_series}

df = pd.DataFrame(data)
print(df)

Explanation:
1. We first import pandas and alias it as pd.
2. We create two Series: one for the City column and one for the State column.
3. We store these Series in a dictionary where keys are column names.
4. We pass the dictionary to pd.DataFrame() to create the DataFrame.
5. Finally, we print the DataFrame to display the output in tabular form.
This produces the required DataFrame exactly as shown.
Was this answer helpful?
0
0

Top Questions on Python Libraries

View More Questions