Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Pandas Cookbook

You're reading from   Pandas Cookbook Practical recipes for scientific computing, time series, and exploratory data analysis using Python

Arrow left icon
Product type Paperback
Published in Oct 2024
Publisher Packt
ISBN-13 9781836205876
Length 404 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
William Ayd William Ayd
Author Profile Icon William Ayd
William Ayd
Matthew Harrison Matthew Harrison
Author Profile Icon Matthew Harrison
Matthew Harrison
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. pandas Foundations FREE CHAPTER 2. Selection and Assignment 3. Data Types 4. The pandas I/O System 5. Algorithms and How to Apply Them 6. Visualization 7. Reshaping DataFrames 8. Group By 9. Temporal Data Types and Algorithms 10. General Usage and Performance Tips 11. The pandas Ecosystem 12. Index

Series

The basic building block in pandas is a pd.Series, which is a one-dimensional array of data paired with a pd.Index. The index labels can be used as a simplistic way to look up values in the pd.Series, much like the Python dictionary built into the language uses key/value pairs (we will expand on this and much more pd.Index functionality in Chapter 2, Selection and Assignment).

The following section demonstrates a few ways of creating a pd.Series directly.

How to do it

The easiest way to construct a pd.Series is to provide a sequence of values, like a list of integers:

pd.Series([0, 1, 2])
0    0
1    1
2    2
dtype: int64

A tuple is another type of sequence, making it valid as an argument to the pd.Series constructor:

pd.Series((12.34, 56.78, 91.01))
0    12.34
1    56.78
2    91.01
dtype: float64

When generating sample data, you may often reach for the Python range function:

pd.Series(range(0, 7, 2))
0    0
1    2
2    4
3    6
dtype: int64

In all of the examples so far, pandas will try and infer a proper data type from its arguments for you. However, there are times when you will know more about the type and size of your data than can be inferred. Providing that information explicitly to pandas via the dtype= argument can be useful to save memory or ensure proper integration with other typed systems, like SQL databases.

To illustrate this, let’s use a simple range argument to fill a pd.Series with a sequence of integers. When we did this before, the inferred data type was a 64-bit integer, but we, as developers, may know that we never expect to store larger values in this pd.Series and would be fine with only 8 bits of storage (if you do not know the difference between an 8-bit and 64-bit integer, that topic will be covered in Chapter 3, Data Types). Passing dtype="int8" to the pd.Series constructor will let pandas know we want to use the smaller data type:

pd.Series(range(3), dtype="int8")
0    0
1    1
2    2
dtype: int8

A pd.Series can also have a name attached to it, which can be specified via the name= argument (if not specified, the name defaults to None):

pd.Series(["apple", "banana", "orange"], name="fruit")
0     apple
1     banana
2     orange
Name: fruit, dtype: object
You have been reading a chapter from
Pandas Cookbook - Third Edition
Published in: Oct 2024
Publisher: Packt
ISBN-13: 9781836205876
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime