Pandas — Indexing
Set index
import pandas as pd
df = pd.DataFrame({'name': ['Alice', 'Bob'], 'age': [25, 30]})
df = df.set_index('name')
Reset index
df = df.reset_index()
MultiIndex
arrays = [['A', 'A', 'B', 'B'], ['one', 'two', 'one', 'two']]
index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second'])
df = pd.DataFrame({'data': [1, 2, 3, 4]}, index=index)
Reindex
df = df.reindex(['Alice', 'Bob', 'Charlie'])
Rename
df = df.rename(columns={'name': '姓名', 'age': '年龄'})
Mini Practice
- Set and reset index
- Create MultiIndex
- Reindex data
- Rename columns
Up Next
Continue with Missing Data - Handling NaN values.
Related Topics
Frequently Asked Questions about Indexing
What is Indexing in Pandas?
Indexing is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Indexing?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Indexing.
Why is Indexing important in Pandas?
Indexing is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.