Fundamentals 11 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Pandas Series: Creation, Access, Attributes, and Common Functions

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.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

<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.

data analysisfunctionspandasNumPyseriesattributes
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.