Bird
Raised Fist0
Data Analysis Pythondata~5 mins

DataFrame structure (index, columns, values) in Data Analysis Python

Choose your learning style10 modes available
Introduction

A DataFrame is like a table that helps you organize data in rows and columns. Knowing its structure helps you find and use data easily.

When you want to see how your data is arranged before analyzing it.
When you need to select specific rows or columns for your work.
When you want to check the labels of rows and columns to avoid mistakes.
When you want to understand the shape and size of your data.
When you want to extract just the values without the labels for calculations.
Syntax
Data Analysis Python
df.index
# Shows row labels

df.columns
# Shows column labels

df.values
# Shows data values as an array

df.index gives you the row names or numbers.

df.columns gives you the column names.

Examples
This shows the row labels, which by default are numbers starting at 0.
Data Analysis Python
import pandas as pd

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

print(df.index)
This shows the column names: 'Name' and 'Age'.
Data Analysis Python
print(df.columns)
This shows the actual data inside the table as a list of lists.
Data Analysis Python
print(df.values)
Sample Program

This program creates a simple table of cities and their populations, then prints the row labels, column names, and the data values.

Data Analysis Python
import pandas as pd

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

print('Index:', df.index)
print('Columns:', df.columns)
print('Values:', df.values)
OutputSuccess
Important Notes

The index helps you identify rows uniquely.

The columns help you identify what each column means.

The values give you the raw data without labels.

Summary

A DataFrame has three main parts: index (rows), columns, and values (data).

Use df.index to see row labels, df.columns for column names, and df.values for the data.

Understanding these helps you work with data clearly and avoid mistakes.

Practice

(1/5)
1. Which part of a DataFrame represents the labels for the rows?
easy
A. Values
B. Columns
C. Index
D. Cells

Solution

  1. Step 1: Understand DataFrame parts

    A DataFrame has three main parts: index (row labels), columns (column names), and values (data).
  2. Step 2: Identify row labels

    The row labels are called the index, which helps identify each row uniquely.
  3. Final Answer:

    Index -> Option C
  4. Quick Check:

    Row labels = Index [OK]
Hint: Rows are labeled by the index in a DataFrame [OK]
Common Mistakes:
  • Confusing columns with index
  • Thinking values are labels
  • Mixing cells with index
2. Which of the following is the correct way to get the column names of a DataFrame named df?
easy
A. df.columns
B. df.index
C. df.values
D. df.names

Solution

  1. Step 1: Recall DataFrame attributes

    To get column names, use the columns attribute of the DataFrame.
  2. Step 2: Match correct syntax

    The correct syntax is df.columns. Other options refer to rows or data.
  3. Final Answer:

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

    Columns = df.columns [OK]
Hint: Use df.columns to get column names quickly [OK]
Common Mistakes:
  • Using df.index for columns
  • Trying df.names which doesn't exist
  • Using df.values for columns
3. Given the DataFrame df below:
   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

What will df.values[1, 2] return?
medium
A. 5
B. 2
C. 6
D. 8

Solution

  1. Step 1: Understand DataFrame values and indexing

    df.values returns the data as a 2D array. The first index is the row, the second is the column.
  2. Step 2: Locate the value at row 1, column 2

    Row 1 is the second row: [2, 5, 8]. Column 2 is the third column, value 8.
  3. Final Answer:

    8 -> Option D
  4. Quick Check:

    df.values[1, 2] = 8 [OK]
Hint: Remember indexing starts at 0 for rows and columns [OK]
Common Mistakes:
  • Mixing row and column indexes
  • Using df.iloc instead of df.values
  • Off-by-one errors in indexing
4. What is wrong with this code to get the row labels of a DataFrame df?
row_labels = df.values
medium
A. It returns an empty list
B. It returns the data values, not the row labels
C. It returns the column names instead
D. It causes a syntax error

Solution

  1. Step 1: Understand what df.values returns

    df.values returns the data inside the DataFrame as a numpy array, not the row labels.
  2. Step 2: Identify correct attribute for row labels

    Row labels are stored in df.index, not df.values.
  3. Final Answer:

    It returns the data values, not the row labels -> Option B
  4. Quick Check:

    df.values ≠ row labels [OK]
Hint: Use df.index for rows, not df.values [OK]
Common Mistakes:
  • Confusing df.values with df.index
  • Expecting df.values to give labels
  • Using wrong attribute names
5. You have a DataFrame df with custom row labels ["a", "b", "c"] and columns ["X", "Y"]. Which code correctly extracts the data values as a 2D array?
hard
A. df.values
B. df.index
C. df.columns
D. df.data

Solution

  1. Step 1: Recall DataFrame parts

    Index holds row labels, columns hold column names, and values hold the actual data.
  2. Step 2: Identify correct attribute for data values

    To get the data as a 2D array, use df.values. Other options do not return data values.
  3. Final Answer:

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

    Data values = df.values [OK]
Hint: Use df.values to get data regardless of labels [OK]
Common Mistakes:
  • Using df.index or df.columns for data
  • Trying df.data which doesn't exist
  • Confusing labels with values
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