Sub topic 1
What you are selecting from: index, columns, and values
Every selection in pandas starts from the same object. This chapter describes the three parts of a DataFrame so that later chapters on .loc, .iloc, masks, and query have a clear model to build on.
Prerequisites
This chapter assumes you have the following:
- Basic Python syntax, including lists, dictionaries, and slices.
- pandas installed and imported as
pd. - Some familiarity with creating a DataFrame from a dictionary.
The three parts of a DataFrame
A DataFrame is a two-dimensional table. It has three parts that you will refer to constantly.
- The index holds the row labels. Each row has one label.
- The columns hold the column labels. Each column has one name.
- The values are the data in the cells, stored as one array per column.
Selection is the act of using labels or positions from the index and columns to pick out part of the values.
Building the example
The later chapters reuse this DataFrame. The index is made of strings, which makes the difference between labels and positions easy to see.
import pandas as pd
df = pd.DataFrame(
{
"city": ["Oslo", "Lima", "Pune", "Cairo", "Quito"],
"country": ["Norway", "Peru", "India", "Egypt", "Ecuador"],
"population_k": [709, 10092, 3124, 10230, 2800],
"continent": ["Europe", "South America", "Asia", "Africa", "South America"],
},
index=["a", "b", "c", "d", "e"],
)
print(df.index)
print(df.columns)
print(df.dtypes)The output is:
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
Index(['city', 'country', 'population_k', 'continent'], dtype='object')
city object
country object
population_k int64
continent object
dtype: objectHere is how to read it:
df.indexlists the row labelsatoe. If you do not passindex=, pandas uses the integers0, 1, 2, ....df.columnslists the column names. It is also anIndexobject.df.dtypesshows one data type per column. Text columns have dtypeobject.population_khas dtypeint64. Depending on your pandas version, text columns may instead show asstr.
Why the index matters for selection
The index is not just a display feature. It is the set of labels that .loc looks up, and it stays attached to the data when you filter. If you keep rows b and d, they are still called b and d. They do not become 0 and 1.
Each row also has a position, counted from 0 in the current order. Row b has position 1. Labels and positions are two separate ways to point at the same row. When the index is the default integers they look the same, which hides the difference. A string index makes them visibly different, which is why this tutorial uses one.
Each column is a Series
Each column of a DataFrame is a Series, which is a one-dimensional labeled array. Its index is the DataFrame's index, so the Series and the DataFrame share the same row labels. This is why a comparison such as df["population_k"] > 3000 gives you a result with the labels a to e. The later chapters on boolean masks depend on this.
Because each column has its own dtype, a column holds one kind of data. A row, on the other hand, can mix kinds. This is why selecting by column is cheap and natural, and why dtypes is reported per column.
Common mistakes
- Treating the index as a regular column. The index is separate from
df.columns, so"a"is not a column name. - Assuming the row label equals the row position. This holds only for the default integer index on an unfiltered DataFrame.
- Reading
objectas "mixed". It usually means strings, though it can hold any Python objects.
Exercise
In the example DataFrame, what is the label of the row at position 3, and what is the position of the column continent?
Show answer
The row at position 3 has the label "d" (the city Cairo). The column "continent" is at position 3, because positions count from 0 across city, country, population_k, continent.
Exercise
After you keep only the rows where population_k is above 3000, does the row for Pune keep the label "c"?
Show answer
Yes. Filtering keeps the original index labels, so Pune is still labeled "c", even though its position in the result is different from its position in the original.