Series
Basics
A Series
is a straightforward one-dimensional pandas
object that is created with indices. There are a lot of 1D arrays, but Series
is designed to be compatible with all the pandas
functionalities, making things much easier on us.
my_first_series = pd.Series([5, 10, 15, 20], index=['a', 'b', 'c', 'd'])
print(my_first_series['c'])
15
You can also create a Series
without passing the index
argument. resulting in the default 0-index that is standard for Python at large:
my_second_series = pd.Series([5, 10, 15, 20])
print(my_second_series[2])
15
As we mentioned earlier, other pandas
functions can be used in conjunction with Series
. In the following example, we use the idxmax
function to yield the index of the largest value in the series.
long_list = np.random.randint(low=1, high=10000, size=100)
long_series = pd.Series(long_list)
biggest_value_index = long_series.idxmax()
print(long_series[biggest_value_index])
Knowing how to use a Series
is crucial for understanding and manipulating DataFrames
properly. While we use the term column to describe the vertical dimension of DataFrames
, the data type is a Series
; df.A
or df['A']
will return a Series
, not a list. By extension, we can think of a DataFrame
as a collection of 2 or more Series
objects.