0
0
Pandasdata~10 mins

columns and index attributes in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - columns and index attributes
Create DataFrame
Access .columns attribute
Get list of column names
Access .index attribute
Get row labels (index)
Use or modify columns/index
DataFrame updated or queried
Start with a DataFrame, then use .columns to see or change column names, and .index to see or change row labels.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
  'A': [1, 2],
  'B': [3, 4]
})

cols = df.columns
idx = df.index
Create a DataFrame with columns A and B, then get its columns and index attributes.
Execution Table
StepActionValue of df.columnsValue of df.indexNotes
1Create DataFrame with columns A and BIndex(['A', 'B'], dtype='object')RangeIndex(start=0, stop=2, step=1)DataFrame has 2 columns and default index 0,1
2Access df.columnsIndex(['A', 'B'], dtype='object')RangeIndex(start=0, stop=2, step=1)Columns attribute shows column names
3Access df.indexIndex(['A', 'B'], dtype='object')RangeIndex(start=0, stop=2, step=1)Index attribute shows row labels
4Modify df.columns to ['X', 'Y']Index(['X', 'Y'], dtype='object')RangeIndex(start=0, stop=2, step=1)Column names changed
5Modify df.index to [10, 20]Index(['X', 'Y'], dtype='object')Int64Index([10, 20], dtype='int64')Row labels changed
6Access df.columns and df.index after changesIndex(['X', 'Y'], dtype='object')Int64Index([10, 20], dtype='int64')Attributes reflect updates
7End of traceIndex(['X', 'Y'], dtype='object')Int64Index([10, 20], dtype='int64')Execution complete
💡 All steps executed, showing how columns and index attributes are accessed and modified.
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 5Final
df.columnsN/AIndex(['A', 'B'], dtype='object')Index(['X', 'Y'], dtype='object')Index(['X', 'Y'], dtype='object')Index(['X', 'Y'], dtype='object')
df.indexN/ARangeIndex(start=0, stop=2, step=1)RangeIndex(start=0, stop=2, step=1)Int64Index([10, 20], dtype='int64')Int64Index([10, 20], dtype='int64')
Key Moments - 3 Insights
Why does df.columns show an Index object instead of a simple list?
df.columns returns a pandas Index object which behaves like a list but has extra features; see execution_table rows 1 and 2 where df.columns is shown as Index(['A', 'B'], dtype='object').
What happens if I assign a list of different length to df.columns?
Assigning a list with length different from the number of columns will cause an error; in the trace, step 4 changes columns with a list of matching length, which works fine.
How does changing df.index affect the DataFrame?
Changing df.index changes the row labels; see step 5 where index changes from default RangeIndex to Int64Index with values [10, 20].
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of df.index?
AInt64Index([10, 20], dtype='int64')
BIndex(['A', 'B'], dtype='object')
CRangeIndex(start=0, stop=2, step=1)
DNone
💡 Hint
Check the 'Value of df.index' column at step 3 in execution_table.
At which step does df.columns change to ['X', 'Y']?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Value of df.columns' column in execution_table to find when the change happens.
If you assign df.index = [0, 1, 2], what would happen?
AIt will raise an error because length does not match number of rows
BIt will ignore the assignment
CIt will work fine and change index to [0, 1, 2]
DIt will reset columns instead
💡 Hint
Refer to key_moments about assigning index with different length than rows.
Concept Snapshot
DataFrame.columns and DataFrame.index are attributes to access or set column names and row labels.
.columns returns an Index object listing column names.
.index returns an Index object listing row labels.
You can assign new lists to them but lengths must match existing dimensions.
Changing these attributes updates DataFrame labels accordingly.
Full Transcript
This visual execution trace shows how to use the columns and index attributes of a pandas DataFrame. We start by creating a DataFrame with columns 'A' and 'B' and default row index 0 and 1. Accessing df.columns returns an Index object listing the column names, and df.index returns the row labels as a RangeIndex. We then modify df.columns to ['X', 'Y'], changing the column names, and df.index to [10, 20], changing the row labels. The trace shows the values of these attributes at each step, illustrating how they reflect the DataFrame's structure. Key moments clarify why columns and index are Index objects, the importance of matching lengths when assigning new labels, and the effect of changing the index. The quiz questions test understanding of these attribute values at different steps and consequences of incorrect assignments. The snapshot summarizes the usage rules for these attributes.