Search icon CANCEL
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 attributes

Once you have a pd.Series, there are quite a few attributes you may want to inspect. The most basic attributes can tell you the type and size of your data, which is often the first thing you will inspect when reading in data from a data source.

How to do it

Let’s start by creating a pd.Series that has a name, alongside a custom pd.Index, which itself has a name. Although not all of these elements are required, having them will help us more clearly understand what the attributes we access through this recipe are actually showing us:

index = pd.Index(["dog", "cat", "human"], name="animal")
ser = pd.Series([4, 4, 2], name="num_legs", index=index)
ser
animal
dog      4
cat      4
human    2
Name: num_legs, dtype: int64

The first thing users typically want to know about their data is the type of pd.Series. This can be inspected via the pd.Series.dtype attribute:

ser.dtype
dtype('int64')

The name may be inspected via the pd.Series.name attribute. The data we constructed in this recipe was created with the name="num_legs" argument, which is what you will see when accessing this attribute (if not provided, this will return None):

ser.name
num_legs

The associated pd.Index can be accessed via pd.Series.index:

ser.index
Index(['dog', 'cat', 'human'], dtype='object', name='animal')

The name of the associated pd.Index can be accessed via pd.Series.index.name:

ser.index.name
animal

The shape can be accessed via pd.Series.shape. For a one-dimensional pd.Series, the shape is returned as a one-tuple where the first element represents the number of rows:

ser.shape
3

The size (number of elements) can be accessed via pd.Series.size:

ser.size
3

The Python built-in function len can show you the length (number of rows):

len(ser)
3
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 €18.99/month. Cancel anytime