Creating Series
A Series can be made from a list, a dictionary, a NumPy array or even a single value.
In this page:
Syntax
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
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
- Providing an index whose length differs from the data
- Forgetting the name attribute
- 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: