Python - Create a pandas series from a dictionary

By IncludeHelp Last updated : January 11, 2024

Problem statement

Given a Python dictionary, write Python code to create a Pandas series from it.

Creating a series from a dictionary

To create a pandas series from a dictionary, you can use the pandas.Series() method and pass the dictionary in it as the data of Series.

Python program to create a series from a dictionary

# importing module
import pandas as pd

# creating a dictionary
student_dict = {"Name": "Alex", "Age": 21, "City": "New York"}

# creating series from a dictionary
series = pd.Series(student_dict)

# printing series
print(series)

Output

The output of the above program is:

Name        Alex
Age           21
City    New York
dtype: object

Creating a series from a dictionary with indexes

You can also specify the indexes during creating a series from a dictionary. To assign indexes to a series, pass the indexes to the index attribute inside pandas.Series() method.

Note

If an index attribute with the indexes (custom keys) is passed, the values in data (dictionary) corresponding to the labels in the index will be pulled out. If any index value (key) does not match with the data, the NaN will be printed.

Python program to create a series from a dictionary with indexes

# importing module
import pandas as pd

# creating a dictionary
student_dict = {"Name": "Alex", "Age": 21, "City": "New York"}

# creating series from a dictionary
series = pd.Series(
    student_dict, index=["Name", "City", "Age", "City", "Age", "Name", "ID"]
)

# printing series
print(series)

Output

The output of the above program is:

Name        Alex
City    New York
Age           21
City    New York
Age           21
Name        Alex
ID           NaN
dtype: object

Creating a series of different data type from a dictionary

You can also specify the data type of the output series while creating a series from a dictionary. To specify the data type of the output series, pass the data type with dtype attribute inside pandas.Series() method.

Python program to create a series of different data type from a dictionary

# importing module
import pandas as pd

# creating a dictionary
student_dict = {"a": 10, "b": 20, "c": 30}

# creating series from a dictionary
series = pd.Series(student_dict, dtype="float")

# printing series
print(series)

Output

The output of the above program is:

a    10.0
b    20.0
c    30.0
dtype: float64

Giving a name to the series created from a dictionary

You can also set a name to the output series by specifying the series name with the name attribute inside pandas.Series() method.

Python program to create a series from a dictionary and give a name to it

# importing module
import pandas as pd

# creating a dictionary
student_dict = {"Name": "Alex", "Age": 21, "City": "New York"}

# creating series from a dictionary
series = pd.Series(student_dict, name="student data")

# printing series
print(series)

Output

The output of the above program is:

Name        Alex
Age           21
City    New York
Name: student data, dtype: object

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.