0
0
Pandasdata~10 mins

Specifying column names and index in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Specifying column names and index
Start with raw data
Create DataFrame
Specify column names
Specify index
DataFrame ready with named columns and index
This flow shows how raw data is turned into a DataFrame by specifying column names and an index for easy access.
Execution Sample
Pandas
import pandas as pd

data = [[10, 20], [30, 40]]
columns = ['A', 'B']
index = ['row1', 'row2']

df = pd.DataFrame(data, columns=columns, index=index)
print(df)
Creates a DataFrame from a list of lists, specifying column names and row index labels.
Execution Table
StepActionDataColumnsIndexResulting DataFrame
1Start with raw data[[10, 20], [30, 40]]NoneNoneDataFrame not created yet
2Specify columns[[10, 20], [30, 40]]['A', 'B']NoneDataFrame with columns A, B, default index 0,1
3Specify index[[10, 20], [30, 40]]['A', 'B']['row1', 'row2']DataFrame with columns A, B and index row1, row2
4Print DataFrame[[10, 20], [30, 40]]['A', 'B']['row1', 'row2'] A B row1 10 20 row2 30 40
💡 DataFrame created with specified columns and index; printing shows final structure.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
data[[10, 20], [30, 40]][[10, 20], [30, 40]][[10, 20], [30, 40]][[10, 20], [30, 40]]
columnsNone['A', 'B']['A', 'B']['A', 'B']
indexNoneNone['row1', 'row2']['row1', 'row2']
dfNoneDataFrame with columns A,B and default indexDataFrame with columns A,B and index row1,row2Same as previous, printed
Key Moments - 2 Insights
Why do we specify columns separately instead of relying on default numbers?
Specifying columns names helps us identify data easily by meaningful labels instead of numbers, as shown in execution_table step 2 where columns change from None to ['A', 'B'].
What happens if we don't specify an index?
If index is not specified, pandas uses default numeric index 0,1,... as in execution_table step 2. Specifying index in step 3 replaces this with custom labels.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the index of the DataFrame?
A[0, 1]
B['row1', 'row2']
C['A', 'B']
DNone
💡 Hint
Check the 'Index' column in execution_table row for step 3.
At which step does the DataFrame get column names assigned?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Columns' column in execution_table to see when columns change from None.
If we remove the index parameter, what will be the index after step 3?
A['row1', 'row2']
B['A', 'B']
C[0, 1]
DNone
💡 Hint
Refer to key_moments explanation about default index when index is not specified.
Concept Snapshot
Create DataFrame with pd.DataFrame(data, columns=cols, index=idx)
Columns label each data column for clarity
Index labels rows for easy access
If index not given, default numeric index used
Print DataFrame to see structure
Full Transcript
We start with raw data as a list of lists. Then we create a DataFrame by specifying column names to label each column clearly. Next, we specify an index to label rows. If index is not specified, pandas uses default numeric index. Finally, printing the DataFrame shows the data with named columns and index labels.