Skip to content
Home » pandas Part 3 – Creating Series Objects

pandas Part 3 – Creating Series Objects

Spread the love

series objects

In the previous part we introduced the Series class, which is one of the fundamental data types in pandas. Let’s quickly recreate the Series object from the previous part:

In [1]:
import numpy as np
import pandas as pd

nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79])
nums
Out[1]:
0    2.35
1    4.11
2    0.87
3    2.76
4    3.12
5    5.79
dtype: float64

ARBITRARY INDICES

This Series was created with just one argument, the list of float numbers. This is why it has the default indexing starting at 0 with values increasing by one. But you can define the index explicitly, passing it as the second argument. The values do not have to be in order:

In [3]:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = [3, 4, 5, 8, 7, 6])
nums
Out[3]:
3    2.35
4    4.11
5    0.87
8    2.76
7    3.12
6    5.79
dtype: float64

What’s more, index value may be even repeated:

In [4]:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = [4, 4, 4, 6, 9, 6])
nums
Out[4]:
4    2.35
4    4.11
4    0.87
6    2.76
9    3.12
6    5.79
dtype: float64

Finally, they don’t have to be numbers at all. They can be strings if it makes sense:

In [5]:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = ['a', 'b', 'c', 'd', 'e', 'f'])
nums
Out[5]:
a    2.35
b    4.11
c    0.87
d    2.76
e    3.12
f    5.79
dtype: float64

Or, just for demonstration purposes, something like this:

In [6]:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = [True, False, 'index 3', (1, 2, 3), ['a', 'b'], {1: 'a', 2: 'b'}])
nums
Out[6]:
True                2.35
False               4.11
index 3             0.87
(1, 2, 3)           2.76
[a, b]              3.12
{1: 'a', 2: 'b'}    5.79
dtype: float64

This example is not very practical, but it demonstrates that you can use any data type as the index.

If you use indices, which are not integers, you can also use them to access elements and for slicing:

In [11]:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = ['A', 'B', 'C', 'D', 'E', 'F'])

# access element at index E
nums['E']
Out[11]:
3.12
In [12]:
# slice from element at index 'C' to the end
nums['C':]
Out[12]:
C    0.87
D    2.76
E    3.12
F    5.79
dtype: float64

SERIES FROM DICTIONARY

A more practical example would be creating a Series from a dictionary. Then the keys will be the indices:

In [13]:
# a dictionary
salary_dict = {'Martha Williams': 40000,
           'Ben Hacker': 36000,
           'Michael Johnson': 39500,
           'Tess Danvil': 42600,
           'Emily Prat': 32800}

# a Series
salaries = pd.Series(salary_dict)
salaries
Out[13]:
Martha Williams    40000
Ben Hacker         36000
Michael Johnson    39500
Tess Danvil        42600
Emily Prat         32800
dtype: int64

Naturally, you can use these indices to access elements and for slicing:

In [14]:
# access Tess Danvil's salary
salaries['Tess Danvil']
Out[14]:
42600
In [15]:
# slice from Ben Hacker to the end, every other element
salaries['Ben Hacker'::2]
Out[15]:
Ben Hacker     36000
Tess Danvil    42600
dtype: int64

You can use dictionary methods to access just the keys or key-value pairs as tuples:

In [3]:
# just the keys
salaries.keys()
Out[3]:
Index(['Martha Williams', 'Ben Hacker', 'Michael Johnson', 'Tess Danvil',
       'Emily Prat'],
      dtype='object')
In [8]:
# key-value pairs as tuples
list(salaries.items())
Out[8]:
[('Martha Williams', 40000),
 ('Ben Hacker', 36000),
 ('Michael Johnson', 39500),
 ('Tess Danvil', 42600),
 ('Emily Prat', 32800)]

If you want to check whether a particular key is in the Series object, you can use the membership operators:

In [9]:
'Ben Hacker' in salaries
Out[9]:
True
In [10]:
'Steve McKenzie' not in salaries
Out[10]:
True

You can also modify Series objects and add new ones just like you do with dictionaries:

In [11]:
# let's modify an object
salaries['Tess Danvil'] = 49000

# let's add a new object
salaries['Steve McKenzie'] = 31400

salaries
Out[11]:
Martha Williams    40000
Ben Hacker         36000
Michael Johnson    39500
Tess Danvil        49000
Emily Prat         32800
Steve McKenzie     31400
dtype: int64

If you create a Series from a dictionary, you can explicitly indicate which keys you want to be used, how many times and in what order:

In [17]:
pd.Series({'a': 74, 'b': 32, 'c': 21, 'd': 72, 'e': 81, 'f': 36},
         index = ['d', 'f', 'e', 'f'])
Out[17]:
d    72
f    36
e    81
f    36
dtype: int64

EQUAL VALUES

Finally, if you want all the values to be equal, you can create a Series from a scalar:

In [16]:
pd.Series(2.5, index = [10, 20, 30, 40, 50, 60])
Out[16]:
10    2.5
20    2.5
30    2.5
40    2.5
50    2.5
60    2.5
dtype: float64

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of this article:


Spread the love

Leave a Reply