The Anatomy of a DataFrame
What are the components of a DataFrame?
What is the index of a DataFrame?
How does a DataFrame index matter?
Is it mandatory to pass an index when I create a DataFrame?
How do I access a DataFrame index?
Can I edit a DataFrame index?
# 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 0What is a Pandas Series?
How do I create a Series?
How do I access a Series included in DataFrame?
# 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