LambdaLab
Informatics Practices · Class 12 · Python Pandas · Series
PandasSeries⏱️ 9 min read

Creating a Series

A Series can be born from all sorts of data — a plain list, a NumPy array, a dictionary, even a single value. Every one of them starts with the same import, then a call to pd.Series(...).

Play with the builder below to preview each source, then run the real code in the panels underneath.

Series builder

Pick a source of data — see the code that builds it and the Series it produces.

import pandas as pd
cities = ['Mumbai', 'Delhi', 'Bengaluru', 'Chennai']
s = pd.Series(cities)
print(s)
0Mumbai
1Delhi
2Bengaluru
3Chennai
dtype: object

💡 List: Most common way. Pandas auto-numbers the index from 0.

1Method 1 — The empty Series

Sometimes you need to start with nothing and fill it in later. Calling pd.Series() with no arguments creates an empty Series.

method1_empty.py

2Method 2 — From a Python list

This is the most common way. Pandas automatically creates a default integer index starting from 0.

method2_list.py

Beyond the default: pass index=... to give every value a meaningful label of your own.

method2_custom_index.py
Note
The index list must be the same length as the data list — one label per value.

3Method 3 — From a NumPy ndarray

Pandas is built on top of NumPy, so turning an array into a Series is seamless and efficient.

method3_ndarray.py

4Method 4 — From a dictionary

This one is powerful: the dictionary's keys automatically become the Series' index labels, and the values become the data.

method4_dict.py

5Method 5 — From a scalar (single value)

Want a Series where every element is the same? Give a single value — but you must also provide an index, so Pandas knows how many times to repeat it.

method5_scalar.py
Watch Out
Forget the index with a scalar and Pandas has no idea how long the Series should be — you'll get an error. The index does double duty here: it sets both the labels and the length.

6Recap — five ways in

Ways to create a Series
SourceExampleIndex you get
Emptypd.Series()none (empty)
Listpd.Series([1, 2, 3])default 0, 1, 2
List + indexpd.Series(data, index=labels)your labels
NumPy arraypd.Series(np.array([...]))default 0, 1, 2
Dictionarypd.Series({'a': 1})the dict keys
Scalarpd.Series(9, index=[...])your labels (required)
Quick Check

You build a Series from a dictionary. What becomes the index?

Quick Check

Which creation method REQUIRES you to pass an index?