Friday, October 16, 2020

Creating a Series

PyBites Platform | Bite 251. Introducing Pandas Series

We can specify custom indices with the index keyword argument:

In [12]: grades = pd.Series([87, 100, 94], index=['Wally', 'Eva', 'Sam'])
In [13]: grades

Out[13]:

Wally 87
Eva 100
Sam 94
dtype: int64

In this case, we used string indices, but you can use other immutable types, including integers not beginning at 0 and nonconsecutive integers. Again, notice how nicely and concisely pandas formats a Series for display.

If you initialize a Series with a dictionary, its keys become the Series’ indices, and its values become the Series’ element values:

In [14]: grades = pd.Series({'Wally': 87, 'Eva':100, 'Sam': 94})
In [15]: grades

Out[15]:
Wally 87
Eva 100
Sam 94
dtype: int64

In a Series with custom indices, you can access individual elements via square brackets containing a custom index value:

In [16]: grades['Eva']

Out[16]: 100

If the custom indices are strings that could represent valid Python identifiers, pandas automatically adds them to the Series as attributes that you can access via a dot (.), as in:

In [17]: grades.Wally

Out[17]: 87

Series also has built-in attributes. For example, the dtype attribute returns the underlying array’s element type:

In [18]: grades.dtype

Out[18]: dtype('int64')

and the values attribute returns the underlying array:

In [19]: grades.values

Out[19]: array([ 87, 100, 94])

If a Series contains strings, you can use its str attribute to call string methods on the elements. First, let’s create a Series of hardware-related strings:

In [20]: hardware = pd.Series(['Hammer', 'Saw','Wrench'])

In [21]: hardware

Out[21]:
0 Hammer
1 Saw
2 Wrench
dtype: object

Note that pandas also right-aligns string element values and that the dtype for strings is object. Let’s call string method contains on each element to determine whether the value of each element contains a
lowercase 'a':

In [22]: hardware.str.contains('a')

Out[22]:
0 True
1 True
2 False
dtype: bool

Pandas returns a Series containing bool values indicating the contains method’s result for each element — the element at index 2 ('Wrench') does not contain an 'a', so its element in the resulting Series is False. Note that pandas handles the iteration internally for you—another example of functional-style programming. The str attribute provides many string-processing methods that are similar to those in
Python’s string type.

The following uses string method upper to produce a new Series containing the uppercase versions of each element in hardware:

In [23]: hardware.str.upper()

Out[23]:
0 HAMMER
1 SAW
2 WRENCH
dtype: object


Share:

0 comments:

Post a Comment