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

DataFrame attributes

The pd.DataFrame shares many of the attributes of the pd.Series, with some slight differences. Generally, pandas tries to share as many attributes as possible between the pd.Series and pd.DataFrame, but the two-dimensional nature of the pd.DataFrame makes it more natural to express some things in plural form (for example, the .dtype attribute becomes .dtypes) and gives us a few more attributes to inspect (for example, .columns exists for a pd.DataFrame but not for a pd.Series).

How to do it

Much like we did in the previous section, we are going to construct a pd.DataFrame with a custom pd.Index in the rows, while also using custom labels in the columns. This will be more helpful when inspecting the various attributes:

index = pd.Index(["Jack", "Jill"], name="person")
df = pd.DataFrame([
    [24, 180, "red"],
    [42, 166, "blue"],
], columns=["age", "height_cm", "favorite_color"], index=index)
df
           age    height_cm    favorite_color
person
Jack       24     180          red
Jill       42     166          blue

The types of each column can be inspected via the pd.DataFrame.dtypes attribute. This attribute returns a pd.Series where each row shows the data type corresponding to each column in our pd.DataFrame:

df.dtypes
age                int64
height_cm          int64
favorite_color     object
dtype: object

The row index can be accessed via pd.DataFrame.index:

df.index
Index(['Jack', 'Jill'], dtype='object', name='person')

The column index can be accessed via pd.DataFrame.columns:

df.columns
Index(['age', 'height_cm', 'favorite_color'], dtype='object')

The shape can be accessed via pd.DataFrame.shape. For a two-dimensional pd.DataFrame, the shape is returned as a two-tuple where the first element represents the number of rows and the second element represents the number of columns:

df.shape
2     3

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

df.size
6

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

len(df)
2

Join our community on Discord

Join our community’s Discord space for discussions with the authors and other readers:

https://packt.link/pandas

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