← Back to Pandas Course | Chapter 2: Series | Lesson 1 of 7

Creating Series

A Series can be made from a list, a dictionary, a NumPy array or even a single value.

In this page:

  1. Creating Series
Syntax
python
s = pd.Series(data, index=labels, name="series_name")

Creating Series

pd.Series(data, index=..., name=...) builds a Series. Lists and arrays supply the values, while a scalar is repeated across the index. The name attribute labels the Series and becomes the column name when placed in a DataFrame.

Note: You can give the Series a name so that it shows up in DataFrame headers.

Example: Creating Series

python
import pandas as pd
import numpy as np

print(pd.Series([1, 2, 3]))
print(pd.Series(np.arange(3), index=list("abc"), name="values"))
print(pd.Series(7, index=["x", "y"]))

# Output:
# 0    1
# 1    2
# 2    3
# dtype: int64
# a    0
# b    1
# c    2
# Name: values, dtype: int64
# x    7
# y    7
# dtype: int64
Related Topics
Common Mistakes
  1. Providing an index whose length differs from the data
  2. Forgetting the name attribute
  3. Passing a scalar without an index
Chapter Summary
  • pd.Series(data, index, name)
  • Scalars broadcast over the index
  • Index length must match data
  • name labels the Series
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.