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.
<code>series_object = pandas.Series()</code>Example:
<code>import pandas as pd
x = pd.Series()
print(x)</code>Output:
<code>Series([], dtype: float64)</code>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.
<code>import pandas as pd
import numpy as np
info = np.array(['P','a','n','d','a','s'])
a = pd.Series(info)
print(a)</code>Output:
<code>0 P
1 a
2 n
3 d
4 a
5 s
dtype: object</code>From a Dictionary
If a dictionary is provided without an explicit index, the keys are sorted to form the index.
<code># 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)</code>Output:
<code>x 0.0
y 1.0
z 2.0
dtype: float64</code>From a Scalar
A scalar value requires an explicit index; the scalar is repeated to match the index length.
<code>import pandas as pd
import numpy as np
x = pd.Series(4, index=[0, 1, 2, 3])
print(x)</code>Output:
<code>0 4
1 4
2 4
3 4
dtype: int64</code>Accessing Series Data by Position
Elements can be accessed similarly to NumPy arrays using integer positions.
<code>import pandas as pd
x = pd.Series([1,2,3], index=['a','b','c'])
print(x[0])</code>Output:
<code>1</code>Series Object Attributes
Key attributes provide information about a Series:
Attribute
Description
Series.indexDefines the index labels.
Series.shapeTuple representing the data shape.
Series.dtypeData type of the elements.
Series.sizeTotal number of elements.
Series.emptyTrue if the Series is empty.
Series.hasnansTrue if any NaN values are present.
Series.nbytesMemory usage in bytes.
Series.ndimNumber of dimensions.
Series.itemsizeSize of each element in bytes.
Retrieving Index and Values
<code>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)</code>Output:
<code>RangeIndex(start=0, stop=4, step=1)
[2 4 6 8]
Index(['a', 'b', 'c'], dtype='object')
[11.2 18.6 22.5]</code>Retrieving dtype and itemsize
<code>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)</code>Output:
<code>int64
8
float64
8</code>Retrieving Shape
<code>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)</code>Output:
<code>(4,)
(3,)</code>Retrieving ndim, size, and nbytes
<code>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)</code>Output:
<code>1 1
4 3
32 24</code>Checking for Empty Series and NaN Values
<code>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())</code>Output:
<code>False False True
True False False
4 3
3 3</code>Series 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.
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.