Bird
Raised Fist0
Pandasdata~5 mins

columns and index attributes in Pandas

Choose your learning style10 modes available
Introduction
Columns and index attributes help you see and organize the labels of your data in tables. They make it easy to find and work with your data.
When you want to check the names of all columns in your data table.
When you need to rename or reorder columns to make data clearer.
When you want to see or change the row labels (index) for better data access.
When you want to select data by column or row names instead of positions.
When you want to add meaningful labels to your data for easier understanding.
Syntax
Pandas
dataframe.columns

dataframe.index
The columns attribute shows the list of column names in your table.
The index attribute shows the row labels, which can be numbers or names.
Examples
This example creates a simple table and prints the column names and row labels.
Pandas
import pandas as pd

data = {'Name': ['Anna', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)

print(df.columns)
print(df.index)
Here, we rename the columns to new names.
Pandas
df.columns = ['First Name', 'Years']
print(df.columns)
This changes the row labels from numbers to letters.
Pandas
df.index = ['a', 'b']
print(df.index)
Sample Program
This program creates a table of cities and populations, shows the original column and index labels, then renames them and shows the new labels.
Pandas
import pandas as pd

data = {'City': ['Paris', 'London', 'Berlin'], 'Population': [2148000, 8982000, 3769000]}
df = pd.DataFrame(data)

# Show original columns and index
print('Columns:', df.columns)
print('Index:', df.index)

# Rename columns
df.columns = ['City Name', 'Number of People']

# Change index labels
df.index = ['a', 'b', 'c']

print('New Columns:', df.columns)
print('New Index:', df.index)
OutputSuccess
Important Notes
Changing columns or index labels does not change the data itself, only the labels.
You can use these attributes to select data by name, which is easier than using numbers.
The index can be any labels, not just numbers, which helps when your rows have meaningful names.
Summary
Columns attribute shows or sets the names of the data table's columns.
Index attribute shows or sets the row labels for easier data access.
Renaming columns or index helps make data clearer and easier to work with.

Practice

(1/5)
1. What does the columns attribute in a pandas DataFrame represent?
easy
A. The number of rows in the DataFrame
B. The data types of each column
C. The names of the columns in the DataFrame
D. The values of the first row

Solution

  1. Step 1: Understand the role of columns in pandas

    The columns attribute stores the labels for each column in the DataFrame, like the headers in a table.
  2. Step 2: Compare with other attributes

    Other attributes like index store row labels, not column names.
  3. Final Answer:

    The names of the columns in the DataFrame -> Option C
  4. Quick Check:

    columns = column names [OK]
Hint: Columns attribute holds column names, like table headers [OK]
Common Mistakes:
  • Confusing columns with index
  • Thinking columns store data types
  • Assuming columns count rows
2. Which of the following is the correct way to get the row labels of a pandas DataFrame df?
easy
A. df.index
B. df.columns
C. df.labels
D. df.rows

Solution

  1. Step 1: Recall the attribute for row labels

    The index attribute in pandas stores the row labels or indices.
  2. Step 2: Verify other options

    columns is for column names; labels and rows are not valid pandas attributes.
  3. Final Answer:

    df.index -> Option A
  4. Quick Check:

    index = row labels [OK]
Hint: Use df.index to get row labels, not df.columns [OK]
Common Mistakes:
  • Using df.columns to get row labels
  • Trying non-existent attributes like df.labels
  • Confusing rows with index attribute
3. Given the DataFrame df below:
import pandas as pd
df = pd.DataFrame({
  'A': [1, 2],
  'B': [3, 4]
}, index=['x', 'y'])
print(df.columns)

What will be the output?
medium
A. Index(['A', 'B'], dtype='object')
B. Index(['x', 'y'], dtype='object')
C. ['x', 'y']
D. ['A', 'B']

Solution

  1. Step 1: Understand the DataFrame creation

    The DataFrame has columns 'A' and 'B' and row indices 'x' and 'y'.
  2. Step 2: Check what df.columns returns

    df.columns returns an Index object listing the column names, which are 'A' and 'B'.
  3. Final Answer:

    Index(['A', 'B'], dtype='object') -> Option A
  4. Quick Check:

    df.columns = Index of column names [OK]
Hint: df.columns shows column names as Index object [OK]
Common Mistakes:
  • Confusing columns with index values
  • Expecting a list instead of Index object
  • Mixing up row labels with columns
4. What is wrong with this code snippet?
import pandas as pd
df = pd.DataFrame({'X': [10, 20], 'Y': [30, 40]})
df.index = ['a', 'b', 'c']
medium
A. The DataFrame must have numeric index only
B. DataFrame columns cannot be renamed this way
C. The code is correct and runs without error
D. The index list length does not match the number of rows

Solution

  1. Step 1: Check DataFrame shape

    The DataFrame has 2 rows but the new index list has 3 labels.
  2. Step 2: Understand index assignment rules

    When assigning to df.index, the length must match the number of rows exactly.
  3. Final Answer:

    The index list length does not match the number of rows -> Option D
  4. Quick Check:

    Index length must equal row count [OK]
Hint: Index length must match rows count exactly [OK]
Common Mistakes:
  • Assigning index list with wrong length
  • Thinking columns rename affects index
  • Assuming any list can be index regardless of length
5. You have a DataFrame df with columns ['name', 'age', 'city'] and default numeric index. How do you rename the columns to ['Name', 'Age', 'City'] and set the index to the 'name' column in one step?
hard
A. df.columns = ['Name', 'Age', 'City']; df.set_index('name', inplace=True)
B. df.rename(columns={'name':'Name', 'age':'Age', 'city':'City'}, inplace=True); df.set_index('Name', inplace=True)
C. df.columns = ['Name', 'Age', 'City']; df.index = df['Name']
D. df.rename(index={'name':'Name'}, inplace=True); df.columns = ['Name', 'Age', 'City']

Solution

  1. Step 1: Rename columns properly

    Use df.rename(columns=..., inplace=True) to rename columns safely without losing data.
  2. Step 2: Set the index to the renamed 'Name' column

    Use df.set_index('Name', inplace=True) to set the index to the 'Name' column after renaming.
  3. Final Answer:

    df.rename(columns={'name':'Name', 'age':'Age', 'city':'City'}, inplace=True); df.set_index('Name', inplace=True) -> Option B
  4. Quick Check:

    Rename columns then set index by column name [OK]
Hint: Rename columns with rename(), then set_index() by new name [OK]
Common Mistakes:
  • Assigning columns list directly without rename()
  • Setting index before renaming columns
  • Assigning index to a Series without set_index()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard