Pandas Series: Creation, Access, Attributes, and Common Functions
This article explains what a Pandas Series is, describes its parameters, shows how to create empty or populated series from arrays, dictionaries, or scalar values, demonstrates element access, lists key Series attributes, and introduces useful Series functions with code examples.
Pandas Series Overview
Pandas Series is a one‑dimensional array capable of holding various data types. It can be created from lists, tuples, dictionaries, or scalar values using the pandas.Series() constructor. Each element has an index label, and a Series contains only a single column.
Series Parameters
data : any list, dictionary, or scalar value.
index : unique, hashable labels matching the length of data. If omitted, np.arange(n) is used.
dtype : the data type of the Series.
copy : whether to copy the input data.
Creating Series
Empty Series
An empty Series contains no values and defaults to float64 dtype. series_object = pandas.Series() Example:
import pandas as pd
x = pd.Series()
print(x)Output:
Series([], dtype: float64)Series from Input
You can create a Series from arrays, dictionaries, or scalar values.
From an Array
First import numpy and use np.array(). If no index is supplied, the default range index is used.
import pandas as pd
import numpy as np
info = np.array(['P','a','n','d','a','s'])
a = pd.Series(info)
print(a)Output:
0 P
1 a
2 n
3 d
4 a
5 s
dtype: objectFrom a Dictionary
If a dictionary is provided without an explicit index, the keys are sorted to form the index.
# import the pandas library
import pandas as pd
import numpy as np
info = {'x': 0., 'y': 1., 'z': 2.}
a = pd.Series(info)
print(a)Output:
x 0.0
y 1.0
z 2.0
dtype: float64From a Scalar
A scalar value requires an explicit index; the scalar is repeated to match the index length.
import pandas as pd
import numpy as np
x = pd.Series(4, index=[0, 1, 2, 3])
print(x)Output:
0 4
1 4
2 4
3 4
dtype: int64Accessing Series Data by Position
Elements can be accessed similarly to NumPy arrays using integer positions.
import pandas as pd
x = pd.Series([1,2,3], index=['a','b','c'])
print(x[0])Output:
1Series Object Attributes
Key attributes provide information about a Series:
Attribute
Description Series.index Defines the index labels. Series.shape Tuple representing the data shape. Series.dtype Data type of the elements. Series.size Total number of elements. Series.empty True if the Series is empty. Series.hasnans True if any NaN values are present. Series.nbytes Memory usage in bytes. Series.ndim Number of dimensions. Series.itemsize Size of each element in bytes.
Retrieving Index and Values
import numpy as np
import pandas as pd
x = pd.Series(data=[2,4,6,8])
y = pd.Series(data=[11.2,18.6,22.5], index=['a','b','c'])
print(x.index)
print(x.values)
print(y.index)
print(y.values)Output:
RangeIndex(start=0, stop=4, step=1)
[2 4 6 8]
Index(['a', 'b', 'c'], dtype='object')
[11.2 18.6 22.5]Retrieving dtype and itemsize
import numpy as np
import pandas as pd
a = pd.Series(data=[1,2,3,4])
b = pd.Series(data=[4.9,8.2,5.6], index=['x','y','z'])
print(a.dtype)
print(a.itemsize)
print(b.dtype)
print(b.itemsize)Output:
int64
8
float64
8Retrieving Shape
import numpy as np
import pandas as pd
a = pd.Series(data=[1,2,3,4])
b = pd.Series(data=[4.9,8.2,5.6], index=['x','y','z'])
print(a.shape)
print(b.shape)Output:
(4,)
(3,)Retrieving ndim, size, and nbytes
import numpy as np
import pandas as pd
a = pd.Series(data=[1,2,3,4])
b = pd.Series(data=[4.9,8.2,5.6], index=['x','y','z'])
print(a.ndim, b.ndim)
print(a.size, b.size)
print(a.nbytes, b.nbytes)Output:
1 1
4 3
32 24Checking for Empty Series and NaN Values
import numpy as np
import pandas as pd
a = pd.Series(data=[1,2,3,np.NaN])
b = pd.Series(data=[4.9,8.2,5.6], index=['x','y','z'])
c = pd.Series()
print(a.empty, b.empty, c.empty)
print(a.hasnans, b.hasnans, c.hasnans)
print(len(a), len(b))
print(a.count(), b.count())Output:
False False True
True False False
4 3
3 3Series Functions
Commonly used Pandas Series functions include:
Function
Description Pandas Series.map() Maps values based on a common column between two Series. Pandas Series.std() Computes the standard deviation of a numeric Series. Pandas Series.to_frame() Converts a Series into a DataFrame. Pandas Series.value_counts() Returns a Series containing counts of unique values.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
