【发布时间】:2020-05-31 16:54:19
【问题描述】:
我每年使用多种场景预测产品的需求。我有一个 MultiIndexed 数据框(模拟、年、月),需要按其中一个进行过滤(比如说模拟)。
import pandas as pd
idx = pd.MultiIndex.from_tuples([(1,2020,1), (1,2020,2), (2,2020,1), (2,2020,2)],
names=['Simulation', 'Year', 'Month'])
d = {'Apples': [1,2,3,4], 'Organes': [4,5,6,8], 'Lemons': [9,10,11,12]}
df = pd.DataFrame(d, index=idx)
print(df)
Simulation Year Month Apples Oranges Lemons
1 2020 1 1 4 9
1 2 2 5 10
2 2020 1 3 6 11
2 2 4 8 12
如何按模拟过滤?
仅按模拟编号 1 过滤的预期输出
Simulation Year Month Apples Oranges Lemons
1 2020 1 1 4 9
1 2 2 5 10
【问题讨论】:
-
欢迎来到 Stack Overflow,哈维尔!我建议您阅读帮助页面How do I ask a good question?。分享您用于实现的代码的 sn-p 非常重要,请查看How to create a Minimal, Reproducible Example。更多信息,更多可能获得非常好的答案来解决您的问题。
标签: python pandas multi-index