Today I learned about the fundamental pandas data type Series
.
Series
?A Series
is one of the fundamental data types in pandas and is a one-dimensional container for data.
Series
are also indexable, either through integer indices (like the list
or tuple
built-in types),
or through arbitrary hashable labels.
To create a Series
, you just give it an iterable with the data you want:
>>> import pandas as pd
>>> pd.Series([10, 20])
0 10
1 20
dtype: int64
>>> pd.Series(range(3))
0 0
1 1
2 2
dtype: int64
The output above shows two columns, where the first column gives the indices (consecutive non-negative integers by default), and the second column shows the data.
Series
are printed vertically to align with the fact that typically Series
contain related data
that you can often imagine as a column in a table of data.
Series
?If you want to change the labels associated with your data,
you can use the argument index
when creating a Series
:
>>> s = pd.Series(range(3), index=["a", "b", "c"])
>>> s
a 0
b 1
c 2
dtype: int64
>>> s["b"]
1
That's the most straightforward way to do it. Probably, there are others!
Series
labelsOn top of the ability to support arbitrary (hashable) values for the labels of its values,
a Series
does not need unique labels.
When the labels are non-unique and you use a one of those labels to access the Series
,
you access all of the values associated with that label:
>>> s = pd.Series(range(3), index=["a", "b", "a"])
>>> s["a"]
a 0
a 2
dtype: int64
Contrast this with the way the built-in dictionaries work:
>>> d = {"a": 0, "b": 1, "a": 2}
>>> d
{'a': 2, 'b': 1}
>>> d["a"]
2
Notice how the key "a"
is only associated with the value 2
because keys in dictionaries must be unique.
That's it for now! Stay tuned and I'll see you around!
+35 chapters. +400 pages. Hundreds of examples. Over 30,000 readers!
My book βPydon'tsβ teaches you how to write elegant, expressive, and Pythonic code, to help you become a better developer. >>> Download it here ππ.