Book Image

Learning Pandas

By : Michael Heydt
Book Image

Learning Pandas

By: Michael Heydt

Overview of this book

Table of Contents (19 chapters)
Learning pandas
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Arithmetic operations


Arithmetic operations (+, -, /, *, and so on) can be applied either to a Series or between two Series objects. When applied to a single Series, the operation is applied to all of the values in that Series. The following code demonstrates arithmetic operations applied to a Series object by multiplying the values in s3 by 2. The result is a new Series with the new values (s3 is unchanged).

In [46]:
   # multiply all values in s3 by 2
   s3 * 2

Out[46]:
   a    2
   b    4
   c    6
   dtype: int64

The preceding code is also roughly equivalent to the following code, which creates a new series from a scalar value using the index from s3. It has the same result, but it is not as efficient, as alignment is performed between the Series objects instead of a simple vectorization of the multiplication:

In [47]:
   # scalar series using s3's index
   t = pd.Series(2, s3.index)
   s3 * t

Out[47]:
   a    2
   b    4
   c    6
   dtype: int64

To reinforce the point that alignment...