Create a Series from a List, Numpy Array, and Dictionary in Pandas


Pandas is an open-source Python Library providing high-performance data manipulation and analysis tool using its powerful data structures. The name Pandas is derived from the word Panel Data – an Econometrics from Multidimensional data.

Series is a one-dimensional labelled array capable of holding data of any type i.e. integer, string, float, python objects, etc). The axis labels are collectively called index.

To create a series, at first install the pandas library. We use pip to install any library in Python −

pip install pandas

Create a Pandas Series from a List

Example

We will create a series from a List using the series(). The list will be set as a parameter −

import pandas as pd # Create a List myList = [5, 10, 15, 20, 25, 30] # Create a Pandas series using the series() method res = pd.Series(myList) print("Series = \n",res)

Output

Series = 
0     5
1    10
2    15
3    20
4    25
5    30
dtype: int64

Create a Pandas Series from a Numpy Array

To create a series from a numpy array, use the numpy library 

import numpy as np

Example

Let us see an example 

import pandas as pd import numpy as np # Create an array using numpy.array() arr = np.array([5, 10, 15, 20, 25, 30]) # Create a Pandas series using the Numpy array res = pd.Series(arr) print("Series = \n",res)

Output

Series = 
0     5
1    10
2    15
3    20
4    25
5    30
dtype: int64

Create a Pandas Series from a Dictionary

Example

In this example, we will create a series from Dictionary with key-value pairs 

import pandas as pd import numpy as np # Create a Dictionary with Keys and Values d = { "A":"demo", "B": 40, "C": 25, "D": "Yes" } # Create a Pandas series using Dictionary res = pd.Series(d) print("Series = \n",res)

Output

Series = 
A       demo
B       40
C       25
D       Yes
dtype:  object

Updated on: 15-Sep-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements