Fundamentals 5 min read

Advanced Pandas Tutorial: Multi‑Index and apply Method

This tutorial demonstrates how to use Pandas' advanced features, including creating and manipulating multi‑level indexes, selecting data with hierarchical keys, resetting and sorting indexes, and applying custom functions to columns and rows using the apply method, with comprehensive code examples.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Advanced Pandas Tutorial: Multi‑Index and apply Method

Goal : Deeply understand advanced Pandas features.

Learning Content : Multi‑level indexing and the apply method.

Code Examples :

import pandas as pd
# Create example dataset
data = {
    '姓名': ['张三', '李四', '王五', '张三', '赵六', '李四'],
    '部门': ['销售部', '市场部', '技术部', '销售部', '财务部', '市场部'],
    '销售额': [120, 150, 130, 160, 140, 170],
    '成本': [80, 90, 100, 110, 120, 130]
}
df = pd.DataFrame(data)
print(f"示例数据集: \n{df}")
# Set multi‑level index
df.set_index(['部门', '姓名'], inplace=True)
print(f"设置多级索引后的数据集: \n{df}")
# Select data by multi‑level index
selected_data = df.loc[('销售部', '张三')]
print(f"按多级索引选择的数据: \n{selected_data}")
# Partial index selection
selected_data_partial = df.loc['销售部']
print(f"按部分索引选择的数据: \n{selected_data_partial}")
# Reset index
df_reset = df.reset_index()
print(f"重置索引后的数据集: \n{df_reset}")
# Sort index
df_sorted = df.sort_index(level=['部门', '姓名'])
print(f"排序多级索引后的数据集: \n{df_sorted}")
# Apply method examples
def add_ten(x):
    return x + 10
df['销售额加十'] = df['销售额'].apply(add_ten)
print(f"对 '销售额' 列应用函数后的数据集: \n{df}")

def process_row(row):
    row['利润'] = row['销售额'] - row['成本']
    return row
df_processed = df.apply(process_row, axis=1)
print(f"对整个 DataFrame 应用函数后的数据集: \n{df_processed}")
# Lambda expression
df['销售额平方'] = df['销售额'].apply(lambda x: x ** 2)
print(f"使用 lambda 表达式后的数据集: \n{df}")
# Multiple aggregations
df_aggregated = df['销售额'].agg(['sum', 'mean', 'max', 'min'])
print(f"对 '销售额' 列应用多个函数后的结果: \n{df_aggregated}")

Practice : Use the multi‑level index to perform complex data operations, following the same code steps as above.

Summary : By completing this exercise you should have mastered Pandas' multi‑level indexing and the apply method, enabling flexible data management, querying, and custom transformations; further advanced Python data‑processing techniques will be explored in upcoming sessions.

PythonapplyMulti-index
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.