- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a Pandas series from a python dictionary?
We can create a pandas Series object by using a python dictionary by sending the dictionary data to the pandas Series method i.e. pandas.Series(). This pandas Series method will create a new Series object with the keys and value pairs from the python dictionary.
All the keys in the dictionary will become the indices of the Series object, whereas all the values from the key-value pairs in the dictionary will become the values (data) of the Series object.
Let’s see an example to create a pandas Series with a python dictionary, to do this we need to create a python dictionary first.
Example
import pandas as pd # Creating dictionary dictionary = {'A':10, 'B':63, 'D':32, 'C':87} # Creating Series s = pd.Series(dictionary) print(s)
Explanation
Here we initially created a python dictionary with key-value pairs of length 4, after that we passed the dictionary to the pandas Series object. Series is a pandas Series class constructor it will create a Series object, this object has index values from dictionary keys and values from dictionary values.
Output
A 10 B 63 D 32 C 87 dtype: int64
The above output block is the resultant Series object created by the pandas Series method using python dictionary, the data type of each element from the Series is having int64 type.
Let’s create another series object by using the index attribute of the pandas Serie method.
Example
import pandas as pd # Creating dictionary dictionary = {'A':10, 'B':89, 'C':43} # Creating Series s = pd.Series(dictionary, index=['A','B','C', 'D']) print(s)
Explanation
In this example, we have created a series with a python dictionary of length 3, and additionally here we mentioned the index attribute to the Pandas Series object. The input to this index attribute is a list of strings and that has 4 elements in it.
Output
A 10.0 B 89.0 C 43.0 D NaN dtype: float64
In comparison to the previous example here the data type of all data in this series object is float64. This is because the elements in the dictionary and index attribute are not in the same length. Hence the pandas Series method will assign NaN value to that extra index, due to this the dtype of all elements is created to float64 dtype
- Related Articles
- How to Create Pandas Series from a dictionary with indexes in a specific order?
- Create a Series from a List, Numpy Array, and Dictionary in Pandas
- Python Pandas - Create a Series from TimeDeltaIndex
- How to create a series from a list using Pandas?
- How to create a pandas DataFrame using a dictionary?
- How to create a Python dictionary from text file?
- Python program to create a dictionary from a string
- Python Pandas - Create a Series from TimeDeltaIndex and set the name of the resulting Series
- Python Pandas - Create a Series from TimeDeltaIndex and set the index of the resulting Series
- How to create a dictionary in Python?
- How to create Python dictionary from the value of another dictionary?
- How to create Python dictionary from JSON input?
- How to remove NaN from a Pandas Series?
- How to create a Python dictionary from an object's fields?
- How we can create a dictionary from a given tuple in Python?
