- Using
loc
import pandas
# Data
df = pandas.DataFrame({"A": ["a", "a", "b", "b"],
"B": ["x", "y", "x", "y"],
"val": [1, 2, 3, 4]})
df = df.set_index(["A", "B"])
# Extract relevant value
df.loc[("a", "x"), "val"]
# 1
- Using
dict
# Data
dat = {"a": {"x": 1, "y": 2},
"b": {"x": 3, "y": 4}}
# Extract relevant value
dat["a"]["x"]
# 1
How does method 1 compare with method 2 in terms of time complexity? This post's answer mentions that loc
would work in constant time but I'm not clear if that applies to multi-index too.