
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- How to create a Pandas series from a python dictionary?
- How to create a series from a NumPy array?
- How to create a series from a list using Pandas?
- How to Create Pandas Series from a dictionary with indexes in a specific order?
- Create a record array from a (flat) list of array in Numpy
- Create a record array from a (flat) list of array and fetch arrays using names in Numpy
- Python Pandas - Create a Series from TimeDeltaIndex
- Create a record array from a (flat) list of array and set a valid datatype for all in Numpy
- Create a record array from a (flat) list of array and fetch specific values based on index in Numpy
- Create a boolean mask from an array in Numpy
- Create a record array from binary data in Numpy
- 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
- Create a new array from the masked array and return a new reference in Numpy
- Python – Create dictionary from the list
