The Anatomy of a DataFrame

What are the components of a DataFrame?

Question: What are the components of a DataFrame?

Answer: Mainly, DataFrames have two components:

What is the index of a DataFrame?

Question: What is the index of a DataFrame?

Answer: Put simple, it is a Python object associated with a case or cases in a DataFrame. The index is the primary key for a DataFrame.

How does a DataFrame index matter?

Question: How does a DataFrame index matter?

Answer: The index is the primary key for a DataFrame and makes data querying easier and more efficient (we will see this in the querying section).

Is it mandatory to pass an index when I create a DataFrame?

Question: Is it mandatory to pass an index when I create a DataFrame?

Answer: In general, it is not necessary. When we do not pass an index, Pandas will create a mathematical progression and assign it to the index (see the previous code snippet).

How do I access a DataFrame index?

Question: How do I access a DataFrame index?

Answer: index is a DataFrame’s attribute (in a Pythonic sense!). The code snippet below shows how to access the index (line 13). Also, the snippet shows that a RangeIndex object is an iterable object (line 17).

Can I edit a DataFrame index?

Question: Can I edit a DataFrame index?

Answer: Yes, you can. The code snippet below illustrates assigning a DataFrame to an iterable object (line 24). To change the index, it is also possible to use the function .set_index (see the section on manipulating DataFrame columns).

# import pandas with the socially accepted alias 'pd'
>>> import pandas as pd

# create a DataFrame from a dictionary
>>> df = pd.DataFrame.from_dict({"S":["s1", "s2", "s3"], "X":[-99, 8, 0]})
>>> df
    S   X
0  s1 -99
1  s2   8
2  s3   0

# access the index
>>> df.index
RangeIndex(start=0, stop=3, step=1)

# iterate over the index
>>> for item in df.index:
...     print(item)
0
1
2

# change the index
>>> df.index = ["case_1", "case_2", "case_3"]
>>> df
         S   X
case_1  s1 -99
case_2  s2   8
case_3  s3   0

What is a Pandas Series?

Question: What is a Pandas Series?

Answer: A Pandas Series is a one-dimensional object. The columns of a DataFrame are a collection of Series objects.

How do I create a Series?

Question: How do I create a Series?

Answer: It is possible to create a Series using the .series class (see the code snippet below, line 5).

How do I access a Series included in DataFrame?

Question: How do I access a Series included in DataFrame?

Answer: A Series object can be accessed as a DataFrame attribute (see the code snippet below, line 13). It is self-evident that a Series borrows the index of its parent DataFrame (see lines 16 and 24).

# import pandas with the socially accepted alias 'pd'
>>> import pandas as pd

# create a series
>>> S = pd.Series(['s1', 's2', 's3'])
>>> print(S)
0    s1
1    s2
2    s3

# accessing a DataFrame column as a Series
# --+ the data
>>> df = pd.DataFrame.from_dict({"S":["s1", "s2", "s3"], "X":[-99, 8, 0]})
# --+ assign S to the fetched column and print S
>>> S = df.S
>>> print(S)
0    s1
1    s2
2    s3
# --+ amend the index
>>> df.index = ["case_1", "case_2", "case_3"]
# --+ assign S to the fetched column and print S
>>> S = df.S
>>> print(S)
case_1    s1
case_2    s2
case_3    s3