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

DataFrame

While pd.Series is the building block, pd.DataFrame is the main object that comes to mind for users of pandas. pd.DataFrame is the primary and most commonly used object in pandas, and when people think of pandas, they typically envision working with a pd.DataFrame.

In most analysis workflows, you will be importing your data from another source, but for now, we will show you how to construct a pd.DataFrame directly (input/output will be covered in Chapter 4, The pandas I/O System).

How to do it

The most basic construction of a pd.DataFrame happens with a two-dimensional sequence, like a list of lists:

pd.DataFrame([
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
])
    0   1   2
0   0   1   2
1   3   4   5
2   6   7   8

With a list of lists, pandas will automatically number the row and column labels for you. Typically, users of pandas will at least provide labels for columns, as it makes indexing and selecting from a pd.DataFrame much more intuitive (see Chapter 2, Selection and Assignment, for an introduction to indexing and selecting). To label your columns when constructing a pd.DataFrame from a list of lists, you can provide a columns= argument to the constructor:

pd.DataFrame([
    [1, 2],
    [4, 8],
], columns=["col_a", "col_b"])
     col_a    col_b
0    1          2
1    4          8

Instead of using a list of lists, you could also provide a dictionary. The keys of the dictionary will be used as column labels, and the values of the dictionary will represent the values placed in that column of the pd.DataFrame:

pd.DataFrame({
    "first_name": ["Jane", "John"],
    "last_name": ["Doe", "Smith"],
})
            first_name      last_name
0           Jane            Doe
1           John            Smith

In the above example, our dictionary values were lists of strings, but the pd.DataFrame does not strictly require lists. Any sequence will work, including a pd.Series:

ser1 = pd.Series(range(3), dtype="int8", name="int8_col")
ser2 = pd.Series(range(3), dtype="int16", name="int16_col")
pd.DataFrame({ser1.name: ser1, ser2.name: ser2})
             int8_col         int16_col
0            0                0
1            1                1
2            2                2
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