以下代码生成具有分层 MultiIndex 的 Pandas 系列:
import pandas as pd
number_of_classes = 3
number_of_price_brackets = 10
survival_table = pd.Series(
index = pd.MultiIndex.from_tuples(
[(gender,klass,fare)
for gender in range(2)
for klass in range(number_of_classes)
for fare in range(number_of_price_brackets)],
names=['Gender','Class','Price Bracket']
))
但这种方法似乎有点“手动”。有没有更漂亮的方法来生成survival_table
变量?