Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 tools using its powerful data structures. The name Pandas is derived from the word Panel Data - an Econometrics term for multidimensional data.
A Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, Python objects, etc.). The axis labels are collectively called the index.
To create a series, first install the pandas library using pip ?
pip install pandas
Create a Pandas Series from a List
You can create a series from a list by passing the list as a parameter to pd.Series() ?
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)
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, first import both pandas and numpy libraries ?
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)
Series = 0 5 1 10 2 15 3 20 4 25 5 30 dtype: int64
Create a Pandas Series from a Dictionary
When creating a series from a dictionary, the dictionary keys become the index labels and the values become the series data ?
import pandas as pd
# Create a Dictionary with Keys and Values
data_dict = {
"A": "demo",
"B": 40,
"C": 25,
"D": "Yes"
}
# Create a Pandas series using Dictionary
res = pd.Series(data_dict)
print("Series = \n", res)
Series = A demo B 40 C 25 D Yes dtype: object
Comparison of Methods
| Input Type | Index | Best For |
|---|---|---|
| List | Automatic (0, 1, 2...) | Simple sequential data |
| NumPy Array | Automatic (0, 1, 2...) | Numerical computations |
| Dictionary | Dictionary keys | Labeled data |
Conclusion
Pandas Series can be created from lists, NumPy arrays, and dictionaries using pd.Series(). Dictionaries provide custom index labels, while lists and arrays use automatic integer indexing starting from 0.
