pandas Foundations
The pandas library is useful for dealing with structured data. What is structured data? Data that is stored in tables, such as CSV files, Excel spreadsheets, or database tables, is all structured. Unstructured data consists of free-form text, images, sound, or video. If you find yourself dealing with structured data, pandas will be of great utility to you.
pd.Series
is a one-dimensional collection of data. If you are coming from Excel, you can think of this as a column. The main difference is that, like a column in a database, all of the values within pd.Series
must have a single, homogeneous type.
pd.DataFrame
is a two-dimensional object. Much like an Excel sheet or database table can be thought of as a collection of columns, pd.DataFrame
can be thought of as a collection of pd.Series
objects. Each pd.Series
has a homogeneous data type, but the pd.DataFrame
is allowed to be heterogeneous and store a variety of pd.Series
objects with different data types.
pd.Index
does not have a direct analogy with other tools. Excel may offer the closest with auto-numbered rows on the left-hand side of a worksheet, but those numbers tend to be for display purposes only. pd.Index
, as you will find over the course of this book, can be used for selecting values, joining tables, and much more.
The recipes in this chapter will show you how to manually construct pd.Series
and pd.DataFrame
objects, customize the pd.Index
object(s) associated with each, and showcase common attributes of the pd.Series
and pd.DataFrame
that you may need to inspect during your analyses.
We are going to cover the following recipes in this chapter:
- Importing pandas
- Series
- DataFrame
- Index
- Series attributes
- DataFrame attributes